Skip to content

Bug Report: Incomplete test coverage for "Single Element in a Sorted Array" #5409

@AmandaFernandes0701

Description

@AmandaFernandes0701

Bug Report for https://neetcode.io/problems/single-element-in-a-sorted-array

Hi NeetCode team :)
The current test suite for the problem "Single Element in a Sorted Array" allows solutions with incorrect boundary logic to pass. Specifically, implementations that use a restricted "neighbor-check" fail to return the correct result when the single element is at the very beginning (index 0) or the very end of the array. While these solutions fail on LeetCode (15/16 test cases), they pass 100% on NeetCode.io.

Steps to Reproduce
Run the following C# code on the NeetCode.io platform:

  public class Solution {
      public int SingleNonDuplicate(int[] nums) {
          int left = 0;
          int right = nums.Length - 1;
          while(left < right) {
              int middle = left + (right - left) / 2;
            
            // This safety check is too restrictive
            if(middle - 1 >= 0 && middle + 1 < nums.Length) {
                if(nums[middle - 1] != nums[middle] && nums[middle + 1] != nums[middle])
                    return nums[middle];
            }
            
            int indexFirst = (middle > 0 && nums[middle - 1] == nums[middle]) ? middle - 1 : middle;
            if(indexFirst % 2 != 0) right = middle - 1;
            else left = middle + 1;
        }
        return nums[left];
    }
}

Observe that the code passes all NeetCode test cases.

Run the same code against the test case nums = [1, 2, 2, 3, 3].

The code returns 2 instead of the expected 1.

Expected Behavior
The test suite should include cases where the single element is at index 0 and index n-1 to ensure users implement robust boundary checks.

Suggested Test Cases to Add:

Input: [1, 2, 2, 3, 3] | Expected: 1

Input: [1, 1, 2, 2, 3] | Expected: 3

Image Image Image

I noticed a critical issue: when running the test case [1, 2, 2, 3, 3] manually, the console correctly identifies it as a Wrong Answer. However, upon clicking Submit, the solution is marked as Accepted (17/17 test cases).

This indicates that the official submission test suite is missing fundamental edge cases that the manual interpreter is correctly identifying as failures. This provides a false sense of correctness to students and misses the opportunity to teach proper boundary checking in binary search.

I'm sharing this because I really appreciate the NeetCode platform and how it helps students prepare for top-tier interviews. Detecting boundary conditions is a key skill for Big Tech, and having a robust test suite is essential for that learning process.

I hope the team can address this discrepancy as soon as possible to maintain the high quality of the platform's feedback. Looking forward to seeing this fixed!

Best regards, Amanda Fernandes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions