Fix interpolationSearch looping forever when the seek element is above part of the range - #2219
Open
Darkslayer3324j wants to merge 1 commit into
Open
Darkslayer3324j wants to merge 1 commit into
Darkslayer3324j wants to merge 1 commit into
Conversation
…e part of the range If the seek element is missing and larger than the highest element of the current range, the interpolated middle index lands beyond rightIndex, so rightIndex = middleIndex - 1 never shrinks the range. Return -1 when the seek element exceeds the range maximum, and add tests. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
interpolationSearchnever returns for some inputs where the seek element is missing:Trace: the first pass interpolates index 11 (
26 > 24) and setsrightIndex = 10. On the next pass the range is[2 ... 22], butvalueDelta(22) is larger thanrangeDelta(20), so the interpolated index isfloor(22 * 10 / 20) = 11, which is beyondrightIndex.sortedArray[11]is still> 24, sorightIndex = 11 - 1 = 10again, and the loop repeats forever.The function already returns
-1when the seek element is below the range (valueDelta < 0). This adds the symmetric check for a seek element above the range maximum (seekElement > sortedArray[rightIndex]), which also guaranteesvalueDelta <= rangeDelta, so the interpolated index always stays inside[leftIndex, rightIndex]and every iteration shrinks the range.Found by differential fuzzing (comparing 25+ algorithms in
src/algorithmsagainst simple reference implementations on random inputs); the fuzzer stopped on this input because it never finished. I added tests for the hanging input and a few similar ones (missing elements between and above existing values);npx jest src/algorithms/searchandeslintpass.Written with AI assistance (Claude Code); I reproduced the hang, traced the cause above, and ran the tests and linter.