DSA Notes
Master interpolation search — how it improves on binary search using value-based position estimation, when it achieves O(log log n) vs O(n) worst case, the interpolation formula, comparison with binary search, and when to use each.
What Is Interpolation Search?
Interpolation search improves on binary search by estimating *where* the target is likely to be, rather than always checking the middle. It uses the value of the target relative to the range of values to compute a better probe position.
Analogy: When looking up a word in a dictionary, you don't open to the exact middle. If you're looking for "umbrella," you open near the end. If looking for "apple," you open near the beginning. Interpolation search formalizes this intuition.
Complete Implementation
Step-by-Step Trace
| arr[6] = 70 == target | return 6 ✓ (found in 1 step!) |
| Iter 1: mid=4, arr[4]=50 < 70 | left=5 |
| Iter 2: mid=7, arr[7]=80 > 70 | right=6 |
| Iter 3: mid=5, arr[5]=60 < 70 | left=6 |
| Iter 4: mid=6, arr[6]=70 | found! (4 steps) |
For perfectly uniform data, interpolation search is dramatically better.
When Interpolation Search Excels vs Fails
Excels — Uniform Distribution
Fails — Non-Uniform / Exponential Data
| Exponential data | [1, 2, 4, 8, 16, 32, 64, 128, ...] |
| arr[3] = 8 < 64 | left = 4 |
| arr[5] = 32 < 64 | left = 6 |
Comparison: Binary vs Interpolation Search
| Feature | Binary Search | Interpolation Search |
|---|---|---|
| Average case | O(log n) | O(log log n) for uniform |
| Worst case | O(log n) | O(n) |
| Best case | O(1) | O(1) |
| Data requirement | Sorted | Sorted AND uniform |
| Formula | Middle index | Estimated position |
| Safe for all data | ✅ Yes | ❌ No |
| Practical speedup | Baseline | 2-5x for uniform data |
For n = 1,000,000:
- Binary search: ~20 comparisons (log₂ 10⁶ ≈ 20)
- Interpolation (uniform): ~4-5 comparisons (log₂ 20 ≈ 4)
Interpolation Search with Strings
Practical Recommendation
Use Binary Search when
- Data distribution is unknown
- Safety and predictability required
- Worst case must be O(log n)
Use Interpolation Search when
- Data is confirmed to be uniformly distributed
- Data consists of integers or floats (not strings)
- Maximum performance on average is critical
- Worst case O(n) is acceptable (rare non-uniform probe)
In practice, binary search is used far more frequently because:
- Most real data is not uniformly distributed
- The performance difference is small for typical n values
- Binary search's O(log n) worst case is predictable
Interpolation search shines in very specific domains: IP address lookup, timestamp-based retrieval, and sorted integer arrays where uniformity is guaranteed.
Summary
- Interpolation search estimates probe position using value-based extrapolation
- Formula:
pos = left + (target - arr[left]) × (right - left) / (arr[right] - arr[left]) - O(log log n) average for uniformly distributed sorted arrays
- O(n) worst case for skewed distributions — a critical limitation
- Requires both sorted AND approximately uniform data for benefit
- Use binary search for unknown or non-uniform distributions
*Next Lesson: Ternary Search*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Interpolation Search — O(log log n) Search for Uniformly Distributed Data.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, searching, interpolation, search
Related Data Structures & Algorithms Topics