Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codility FrogJmp strange Java score

So I decided to try out Codility. The first task - FrogJmp was ridiculous easy, however to my surprise I scored 44%. Solution, even if correct was obviously unacceptable in terms of performance.

Original solution:

public int solution2(int X, int Y, int D) {
    return (int) Math.ceil((float)(Y -X)/D);
}

So I decided to try it with different code, without floating point arithmetic.

public int solution(int X, int Y, int D) {
    int diff = Y - X;
    if (diff % D == 0)
        return diff /D; 
    else
        return diff/D + 1;

}

This time I got 100%. So I wanted to check the performance myself and wrote simple test:

class Solution {


    public int solution(int X, int Y, int D) {
        int diff = Y - X;
        if (diff % D == 0)
            return diff /D; 
        else
            return diff/D + 1;

    }

    public int solution2(int X, int Y, int D) {
        return (int) Math.ceil((float)(Y -X)/D);
    }


    private static Random ran = new Random(System.currentTimeMillis());

    public static int getRandom(int a, int b){
        return ran.nextInt(b - a + 1) + a; 
    }


    public static void main(String[] args) {

        int size = 1000_000;
        int max = 1000_000_000;

        int[] xs = new int[size];
        int[] ys = new int[size];
        int[] ds = new int[size];

        for (int i = 0; i < size; i++) {
            int y = getRandom(1, max);
            int x = getRandom(1, y);
            int d = getRandom(1, max);
            xs[i] = x;
            ys[i] = y;
            ds[i] = d;
        }

        long start = System.nanoTime();

        Solution sol = new Solution();
        for (int i = 0; i < size; i++) {
            sol.solution2(xs[i], ys[i], ds[i]);
        }

        long diff = System.nanoTime() - start;

        System.out.println("took: " + diff/1000000 + "ms");
    }
}

To my surprise, on my machine the solution 1 takes on average 13ms and solution 2 (the one reported as absolutely ineffective) 10 ms.

What am I missing?

Maybe it has to do something with expected time and space complexity of the tasks.

expected worst-case time complexity is O(1); expected worst-case space complexity is O(1).

Does not solution 2 have constant time & space complexity?

Also I cannot make sense of this results report for 44% solution:

enter image description here

What does it mean??

like image 412
ps-aux Avatar asked Aug 22 '14 21:08

ps-aux


People also ask

How do you pass a performance test in Codility?

PERFORMANCE SCORE: To produce scalable code, a candidate requires good algorithmic knowledge and developed programming skills. In other words, for a candidate's solution to be scalable, it must be suitably efficient and practical when applied to large data-sets.


1 Answers

100/100 solution in C# I just

using System;    
class Solution {
    public int solution(int X, int Y, int D) {        
        return ((Y - X) + D - 1)/D;        
    }
}
like image 157
Nicolás Valenzuela Avatar answered Oct 28 '22 18:10

Nicolás Valenzuela