Skip to content

Fix out-of-range recursion in interpolation_search_by_recursion - #15333

Open
RizgarOzan wants to merge 1 commit into
TheAlgorithms:masterfrom
RizgarOzan:fix-interpolation-search-recursion-bounds
Open

RizgarOzan wants to merge 1 commit into
TheAlgorithms:masterfrom
RizgarOzan:fix-interpolation-search-recursion-bounds

Conversation

@RizgarOzan

Copy link
Copy Markdown

Describe your change

interpolation_search_by_recursion never checks that left <= right (the iterative interpolation_search does, via its while loop), so an item just above the last element, an empty list, or a range reversed by the point > right branch crashes instead of returning None:

>>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 16)
IndexError: list index out of range
>>> interpolation_search_by_recursion([], 1)
IndexError: list index out of range
>>> interpolation_search_by_recursion([0, 3, 6, 12, 14, 15, 20], 10)
RecursionError: maximum recursion depth exceeded

This adds the missing bounds guard. The equal-ends branch is collapsed into a single return to stay within ruff's PLR0911 limit. New doctests cover the three crashing cases plus the symmetric below-minimum case; they fail on master (3 failures) and pass with the fix (python -m doctest -v: 21 passed). A 200k-case random comparison against a brute-force search now passes for both functions. ruff check, ruff format --check, ty check and pre-commit on the file are clean.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@cclauss

cclauss commented Sep 14, 2026

Copy link
Copy Markdown
Member

ON HOLD: Our focus is on merging or closing old pull requests before October 1st.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants