How to see the input values for Codility's (and other sites') tests?

5k Views Asked by At

So, I coded a solution for the MaxProfit exercise (link to the task's description) from lesson 9, in codility for programmers. I'm getting 88% final score (the code is at the bottom, but feel free to read the whole post where I explain in details what my question is about, I believe is a matter of not knowing how to read deeply the page's code, but maybe such information can't be accessed from the client side, I don't know).

This is the 25th exercise I'm solving, all the previous ones were solved with a 100% final score, by me, of course, and I'm still dragging this doubt from many weeks ago, about how to see the actual tests codility's running (not just the test's title, and got/expected value), because sometimes it doesn't show, so you kinda have to guess, even though it always give you an idea of where you might be failing, in the test's title, and what value is get from your code when another one is expected.

When you get details about a failing test, this is the output the pretty much detailed output the page throws at you.

Analysis summary


The following issues have been detected: wrong answers.(This is always shown if there are problems)
[0, 2000] expected 2000 got 0 (You don't always get details about the input but you always get more details about the failing and succeeded tests at the bottom)

After this, you can always see the following details about the tests (even if everything is OK), which is useful but sometimes insufficient.

The following output is (part of) the result for my code in this exercise (you can actually see the page with the full results here)

Detected time complexity: O(N)
▶example length=6✔OK
▶simple_1 V-pattern> sequence, length=7✔OK
▶simple_desc descending and ascending sequence length=5✔OK
▶simple_empty empty and [0,200000] sequence✔OK
▶two_hills two increasing subsequences✘WRONG ANSWER
got 3000 expected 99000-> 1.0.020 WRONG ANSWER, got 3000 expected 99000


As you can see in the last test I got a wrong answer. But I don't see details about the input, and I can't see how it could fail in my code. It only says

two_hills two increasing subsequences and got 3000 expected 99000

But that's not enough! I tested [2,3,7,22, 1, 22, 51] as input, which returns 50, and also [1, 22, 51, 2, 3, 7, 22], also returning 50, and these are two increasing subsequences, am I right? so I don't really get it. But again this question is about, how can I tell which are the values the page is testing for the two_hills and actually all the other tests (as I said earlier, it sometimes shows the input when a test failed, not always, but it never shows the input for succeeded tests).

Can this input be seen in the browser's console? I wasn't able to find it.

Finally, this is my code.


using System;

class Solution {
    public int solution(int[] A) {
        var maxProfit = 0;
        if ( A.Length > 2)
        {
            var minBuyPrice = Math.Min(A[0], A[1]);
            var maxSellPrice = minBuyPrice;
            for (int i = 2; i < A.Length; i++)
            {
                if ( minBuyPrice > A[i])
                {
                    minBuyPrice = A[i];
                    maxSellPrice = A[i];
                }
                maxSellPrice = Math.Max(maxSellPrice, A[i]);
                maxProfit = Math.Max(maxProfit, maxSellPrice - minBuyPrice);
            }
        }
        else if ( A.Length == 2)
        {
            return Math.Max(0, A[1] - A[0]);
        }
        return maxProfit;
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

I got rid of that ugly else if statement and got 100%, this is my final code.

using System;

class Solution {
    public int solution(int[] A) {
        var maxProfit = 0;
        if (A.Length >= 2)
        {
            var minBuyPrice = Math.Min(A[0], A[1]);
            var maxSellPrice = Math.Max(0, A[1] - A[0]);
            maxProfit = maxSellPrice;   

            for (int i = 2; i < A.Length; i++)
            {
                if (minBuyPrice > A[i])
                {
                    minBuyPrice = A[i];
                    maxSellPrice = A[i];
                }
                maxSellPrice = Math.Max(maxSellPrice, A[i]);
                maxProfit = Math.Max(maxProfit, maxSellPrice - minBuyPrice);
            }
        }
        return maxProfit;
    }
}