trekhleb / trekhleb/javascript-algorithms
Infinite loop in interpolationSearch
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 197k
- Forks
- 31k
- PR merge metrics
- No merged PRs in 30d
Description
Details:
Some entries might cause interpolationSearch to fall into an infinite loop.
Step to reproduce: interpolationSearch([0, 3], 6)
How did I find it?
Thanks to property based testing framework fast-check.
The property was the following:
import fc from 'fast-check';
fc.assert(
fc.property(
fc.array(fc.integer()).map(arr => [...arr].sort((a, b) => a - b)),
fc.integer(),
(arr, i) => expect(interpolationSearch(arr, i)).toBe(arr.indexOf(i))
)
)
Or:
for any
arr- sorted array of integers - andi- integer value
interpolationSearch(arr, i)should be equivalent toarr.indexOf(i)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Locate the interpolationSearch entry point and reproduce the reported case, interpolationSearch([0, 3], 6). Then run or extend the relevant tests with the stated sorted-array property; done means searches terminate and return the same result as arr.indexOf(i).
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100