Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

increasing code performance of codility

Tags:

java

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 integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y.

For example, given:
X = 10
Y = 85
D = 30 the function should return 3, 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%,

enter image description here

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 ?

like image 704
bhuvesh Avatar asked Jan 27 '15 19:01

bhuvesh


People also ask

What is performance in Codility?

"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.

What is a good score for Codility?

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.

What can Codility detect?

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.


3 Answers

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))
like image 144
Adria Romero Avatar answered Oct 06 '22 00:10

Adria Romero


Javascript solution, 100/100, and shorter than the existing answer:

function solution(Y, Y, D) {
 return Math.ceil((Y - X) / D);
}
like image 31
nikjohn Avatar answered Oct 06 '22 01:10

nikjohn


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);
    }
}
like image 45
Sarveshwar Singh Avatar answered Oct 06 '22 01:10

Sarveshwar Singh