Skip to content

fix: find interpolation search keys in constant arrays - #1916

Open
pralav-25 wants to merge 1 commit into
TheAlgorithms:masterfrom
pralav-25:fix/interpolation-search-equal-endpoints
Open

pralav-25 wants to merge 1 commit into
TheAlgorithms:masterfrom
pralav-25:fix/interpolation-search-equal-endpoints

Conversation

@pralav-25

Copy link
Copy Markdown

Describe your change:

Interpolation search returns -1 for a present key when the search interval has equal endpoints, including interpolationSearch([7], 7) and interpolationSearch([7, 7, 7], 7). The interpolation calculation divides zero by zero, producing a NaN position.

Return the lower index when the endpoints are equal. The loop's existing bounds check guarantees that the key equals that endpoint value. Add regression cases for singleton and constant arrays, negative and zero values, absent keys, and empty input.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Validation:

  • Before the fix, four new matching-key cases failed with -1.
  • After the fix, all 9 interpolation-search tests pass.
  • Full Vitest suite: 346 files and 11,977 tests pass.
  • Prettier checks pass for both changed files.

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, together with its existing test file.
  • All filenames use the existing project naming conventions.
  • No new algorithm, directory, or dependency is introduced.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.92%. Comparing base (5c39e87) to head (309ce74).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1916   +/-   ##
=======================================
  Coverage   85.91%   85.92%           
=======================================
  Files         379      379           
  Lines       19778    19783    +5     
  Branches     3016     3018    +2     
=======================================
+ Hits        16993    16998    +5     
  Misses       2785     2785           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants