today i heard about this website called codility where a user can give various programming test to check their code's performance.
When I started, they presented me with this sample test,
Task description A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. Count the minimal number of jumps that the small frog must perform to reach its target.
Write a function:
class Solution { public int solution(int X, int Y, int D); }
that, given three integersX
,Y
andD
, returns the minimal number of jumps from positionX
to a position equal to or greater thanY
.For example, given:
X = 10
Y = 85
D = 30
the function should return3
, because the frog will be positioned as follows:after the first jump, at position 10 + 30 = 40
after the second jump, at position 10 + 30 + 30 = 70
after the third jump, at position 10 + 30 + 30 + 30 = 100
Assume that: X, Y and D are integers within the range
[1..1,000,000,000]; X ≤ Y. Complexity: expected worst-case time
complexity is O(1); expected worst-case space complexity is O(1).
The question was pretty straight forward and it took me like 2 minutes to write the solution, which is following,
class Solution {
public int solution(int X, int Y, int D) {
int p = 0;
while (X < Y){
p++;
X = X + D;
}
return p;
}
}
However, the test result shows that the performance of my code is just 20%
and I scored just 55%
,
Here is the link to result, https://codility.com/demo/results/demo66WP2H-K25/
That was so simple code, where I have just used a single while
loop, how could it possibly be make much faster ?
"Performance" corresponds to how their solution performs with large data-sets in terms of running time complexity. To produce scalable code, a candidate requires good algorithmic knowledge and developed programming skills.
To pass the Codility test, you must score at least 60% to have a chance. If you score under 60% your application will be terminated. If you score 100% on the Codility test, you will automatically pass. This is the best way to show the employers that you will be competent in the role.
For any online testing platform, fraud and cheating are one of the biggest challenges to tackle. Codility keeps the integrity of the assessment process by detecting potential suspicious behaviour, which might be related to fraud or impersonation and cheating on the assessment. This opens in a new window.
Here is the 100% total score Python solution:
def solution(X, Y, D):
# write your code in Python 3.6
s = (Y-X)/D
return int(-(-s // 1))
Javascript solution, 100/100, and shorter than the existing answer:
function solution(Y, Y, D) {
return Math.ceil((Y - X) / D);
}
Java(One Line), Correctness 100%, Performance 100%, Task score 100%
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int Y, int D) {
return (int) Math.ceil((double) (Y - X) / (double) D);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With