How to get all the tests from leetcode

When you fail a test on leetcode, it will show you the test. However, if you pass the test, there is no easy way to see the tests. Sometimes, you want to implement the problem in another language, and you want to get all the test cases. Here is what I did to get the tests.

I will use the leetcode problem 55 Jump Game. It says given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Here is the my solution:

class Solution {
  public boolean canJump(int[] nums) {
    int len = nums.length;
    if(len < 2) return true;
    int last_good = len - 1;
    for(int i = len-2; i >= 0; i--){
    if(nums[i] - (last_good - i) >=0)
         last_good = i;
    }
         return last_good == 0;
    }
}

Because the problem is relatively easy, you can pass all tests once. To get the test cases, you can fail the unknown tests. I added this code to check whether I have seen a test:

private boolean find(int[] nums, int[][] tests, int N){
        int lenN = nums.length;
        //compare all arrays
        for(int j = 0; j < N; j++){
            if(Arrays.equals(tests[j], nums))
                return true;
        }
        return false;
    }

Now, I add the two tests cases leetcode provided with the problem, so that I can pass those two tests, but fail anything else:

public boolean canJump(int[] nums) {
    int[][] tests = new int[100][];
    int N = 0;
    tests[N++] = new int[]{2,3,1,1,4};
    tests[N++] = new int[]{3,2,1,0,4};
    int len = nums.length;
    if(len < 2) return true;
    if(!find(nums,tests,N))
                return false;
    int last_good = len - 1;
    for(int i = len-2; i >= 0; i--){
    if(nums[i] - (last_good - i) >=0)
        last_good = i;
    }
        return last_good == 0;
    }

If you notice, I added two tests

int[][] tests = new int[100][];
    int N = 0;
    tests[N++] = new int[]{2,3,1,1,4};
    tests[N++] = new int[]{3,2,1,0,4};

and called

if(!find(nums,tests,N))
   return false;

to fail any new tests.

Now, if you submit the solution, leetcode will show you a new test that you failed. You can copy/pase the test case into your solution. For example: leetcode shows you failed the test [3,0,8,2,0,0,1]. You can add the test as

tests[N++] = new int[]{2,3,1,1,4};
tests[N++] = new int[]{3,2,1,0,4};
tests[N++] = new int[]{3,0,8,2,0,0,1}; //new test

If you submit, leetcode will show you another test and so on. This is tedious, but worked for me. Once a while, the test it huge and if you add the test to your code, leetcode will complain your solution is too big. You can code the property to skip those tests. For example: leetcode shows a test case with a humongous list of 1’s. I added this code to the function find to skip it.

 //check if input is an array of same numbers
 int k=0;
 for(k = 0; k < lenN-1; k++){
    if(nums[k] != nums[k+1]) break;
 }
 if(k >= lenN-1) return true;

Based on the problem and tests, you may have to do other tricks.