See Algorithms
SEE ALGORITHMS

Interpolation Search Visualization

Interpolation Search improves upon Binary Search for sorted arrays whose values are roughly uniformly distributed. Rather than blindly probing the exact midpoint of the interval, it estimates where the target should appear using linear interpolation between arr[low] and arr[high].

Think of searching for a name in a printed phone book: if you are looking for "Baker," you open near the front rather than the middle; for "Young," you flip near the end. When numeric keys are evenly spaced, this proportional probing shrinks the search space dramatically, achieving an impressive O(log log n) average time complexity.


Pseudocode

  low = 0, high = n - 1
  while low <= high and arr[low] <= target <= arr[high]:
      if arr[low] == arr[high]:
          if arr[low] == target: return low
          else: break
      num = (target - arr[low]) * (high - low)
      pos = low + floor((num / (arr[high] - arr[low]))
      if arr[pos] == target: return pos
      else if arr[pos] < target: low = pos + 1
      else: high = pos - 1
  return -1

Visualizer


Probing (pos)EliminatedFound

Control speed:


Time & Space Complexity

Metric / Operation
Complexity
Description
Best CaseO(1)When the initial interpolated probe lands directly on the target element.
Average CaseO(log log n)On uniformly distributed sorted keys, each probe dramatically shrinks the remaining interval.
Worst CaseO(n)On skewed or exponentially spaced values where the probe shrinks the window by only one index per step.
Space ComplexityO(1)Iterative Interpolation Search uses only constant space for low, high, and pos.

Common Interview Questions

Why can Interpolation Search degrade from its O(log log n) average case to O(n)?

It achieves O(log log n) on uniformly distributed sorted keys. On heavily skewed or exponentially growing arrays, the linear probe formula repeatedly lands near one endpoint, shrinking the window by only one element per step (O(n)).

What edge case must be handled before computing the probe index?

If arr[low] == arr[high], the denominator arr[high] - arr[low] is zero. You must check arr[low] == target directly before dividing to prevent division by zero.

How do you fix the O(n) worst case for Interpolation Search?

To eliminate the O(n) worst case, hybrid implementations alternate or fall back to Binary Search when probes fail to shrink the window quickly, guaranteeing O(log n) worst-case time.


💬  Discussion

to join the discussion


Curious to Learn More?

Hand-picked resources to deepen your understanding


Learn DSA on Udemy
Learn DSA on Udemy
As an Udemy Associate, I earn from qualifying purchases.

© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0