trekhleb / trekhleb/javascript-algorithms
Unhandled Edge Case in Binary Search Implementation
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 197k
- Forks
- 31k
- PR merge metrics
- No merged PRs in 30d
Description
The current implementation of the binary search algorithm in the JavaScript codebase does not handle a specific edge case where the target element is present multiple times in the sorted array. This issue is reported to address the potential misbehavior and propose a solution to handle scenarios where the binary search encounters duplicate elements.
Steps to Reproduce:
- Input a sorted array containing duplicate elements.
- Perform a binary search for a target element present multiple times in the array.
- Observe the behavior when the target element is found.
Expected Behavior:
The binary search algorithm should reliably locate and return any occurrence of the target element in the sorted array, providing accurate indices for all instances.
Current Behavior:
The current implementation may not consistently handle scenarios where duplicate elements exist in the sorted array, leading to unpredictable results when searching for the target element.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue does not identify a file or test; first locate the binary search implementation in the JavaScript codebase and reproduce the search with sorted arrays containing duplicate targets. Done means the implementation reliably returns a valid index for a target that appears multiple times, with a regression test covering that case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100