The testcases are too weak


  • 10
    D

    The testcases for this problem are too weak.

    In my first AC solution, I wrote the code

    if (n < k) return false;
    

    , where n is the length of the array.

    Clearly, it is wrong(e.g. nums = {2,2}, k = 3).

    I think the admin should do more serious test before a new problem coming online.


  • 0
    J

    Yes also my first implementation stored the first occurence of the element instead of the previous one for comparison, all the test cases passed.


  • 0

    Thanks @dddxhh, I have just added this test case.


  • 0

    I have added a test case [1,0,1,1], k=1 which should prevent this common mistake from passing.


  • 0
    G

    [2,3,4,3,5]
    5

    There's need a test case when the double elements are closer k-1. This program passes all the test cases now

           if (nums == null || nums.length < 2 || k <= 0) {
                return false;
            }
    
            int left = 0;
            int right = 1;
            while (right < nums.length) {
                if (left < right && nums[left] == nums[right]) {
                    return true;
                }
    
                if (right - left == k) {
                    left++;
                } else {
                    right++;
                }
            }
    
            while (left < nums.length - 1) {
                if (nums[left] == nums[nums.length - 1]) {
                    return true;
                }
                left++;
            }
    
            return false;
    
    

  • 0
    H

    the test case [2,2] 3 is strange. Why is the expected result true?


  • 0
    G

    @haihaicode
    i=0, j=1
    i != j
    a[i]==a[j]==2
    abs(j-i)<3

    So it should be true


  • 0
    S

    Test case with k = 35000 is just stupid. it contradicts to the reason of algorithm.
    IMHO the idea is to create fast efficient algorithm, when duplicates are close each other, not at the opposite side of Universe.
    In that case there is no universal good algorithm.


Log in to reply
 

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