trekhleb / trekhleb/javascript-algorithms

Unhandled Edge Case in Binary Search Implementation

Open
#1,095 1 comment 0 reactions 0 assignees View on GitHub

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:

  1. Input a sorted array containing duplicate elements.
  2. Perform a binary search for a target element present multiple times in the array.
  3. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.