Search code examples
algorithmoutput

Fresh grad hiring test quesiton


It's a multiple choice question based question and the options donot contain -2147483642. the options are 10 ,11 etc

func min_jumps(arr[], start, end)
{
    if(start == end)
        return 0;

    int min = INT_MAX;  // Max value of int

    for(idx = 1; arr[start] >= idx AND end >= start + idx; idx++)
    {
        int jumps = min_jumps(arr, start + idx, end) + 1;
        if(min > jumps)
            min = jumps;
    }
    return min;
}

main()
{
    arr[] = [3, 2, 2, 1, 1, 1, 1, 2, 1, 1],
    ans = min_jumps(arr, 0, lenOfArr);
    print ans;
}

I tried to run the code and it only prints the

-2147483642

Solution

  • The code has a bug.

    When min_jumps doesn't find an answer, it returns INT_MAX.

    When the caller adds 1 to the return value, it wraps around to INT_MIN, or -2147483642.

    You should point this out to whoever maintains the test (IF this is exactly the code on the test).

    You should also point out that AND is not an operator in C or C++.