Skip to content

Handle equal endpoint values in interpolation search - #2779

Open
pralav-25 wants to merge 1 commit into
keon:mainfrom
pralav-25:fix_interpolation_equal_bounds
Open

pralav-25 wants to merge 1 commit into
keon:mainfrom
pralav-25:fix_interpolation_equal_bounds

Conversation

@pralav-25

@pralav-25 pralav-25 commented Sep 27, 2026 •

Copy link
Copy Markdown

interpolation_search([5], 5) and searches in constant arrays raise ZeroDivisionError. Return the lower bound when the endpoint values are equal: the enclosing range check already guarantees that value matches the target.

Adds singleton, duplicate, absent-value and large-integer cases, plus exhaustive membership checks over small sorted arrays.

Validation on Python 3.12.14:

  • The new test file reproduced 6 failing cases on unmodified main.
  • python -m pytest -q: 613 passed.
  • python -m pytest --doctest-modules algorithms/ -q: 480 passed, 1 skipped.
  • ruff check algorithms/ tests/ and git diff --check: passed.

Prepared with AI assistance; the failures and fixes above were reproduced locally.

Summary by CodeRabbit

  • Bug Fixes

    • Interpolation search now correctly handles cases where the search range has equal endpoint values, avoiding a division-by-zero error and returning the appropriate result.
  • Tests

    • Expanded coverage for boundary cases, including empty, singleton, and duplicate-value inputs; missing targets; and searches across large numeric ranges.
    • Added exhaustive checks against expected membership results for small sorted arrays.

@coderabbitai

coderabbitai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 7b67744f-290a-43b9-ad36-df3d29a88a25

📥 Commits

Reviewing files that changed from the base of the PR and between bd1d152 and 57961fb.

📒 Files selected for processing (2)
  • algorithms/searching/interpolation_search.py
  • tests/test_interpolation_boundaries.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

Interpolation search now returns the lower bound when the values at both loop bounds are equal. New tests cover boundary cases and compare results with list membership across small sorted arrays.

Changes

Interpolation search boundaries

Layer / File(s) Summary
Equal-bound handling and validation
algorithms/searching/interpolation_search.py, tests/test_interpolation_boundaries.py
When the values at both loop bounds are equal, the search returns low. Tests cover matching and absent targets, boundary cases, and exhaustive sorted arrays with duplicates.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 57961

No actionable merge-blocking risk is identified; the change is ready for normal checks.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: handling equal endpoint values in interpolation search.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant