Leetcode judge fails for custom test cases: overflows, throws OutOfBounds


  • 0
    I

    OutOfBounds test case:
    Online judge does not check for the case where m or n may be 0
    Passing in m=0, n=5 (Any number)
    Leetcode judge throws an exception for this test case:

    Line 4: java.lang.ArrayIndexOutOfBoundsException: -1
    

    This can be solved by adding a check:

    if(m == 0 || n == 0) {
        return 0;
    }
    

    Overflow test case:
    Description states that m & n can have values upto 100. As the method returns an int, the method overflows for test case m=100 and n=100.
    For this test case, leetcode judge gives a negative answer = -2129403600

    The return type should be a long to support values of m & n ranging upto 100. For a math solution, in case only the result of a multiplication overflows (before dividing it and bringing back to int range), a long value should be used to store intermediate results, and an int value must be returned.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.