DrawCode Algorithm Visualizer • Searching • Medium

Exponential Search

Tags: Searching, Unbounded, Sorted Array

1. One-Liner

Exponential search (also called doubling search) finds a sorted target by probing indices 1, 2, 4, 8, … until the value is bracketed, then runs binary search in that range.


2. The Problem It Solves

When the array is sorted but length is unknown or you want O(log i) behavior where i is the index of the answer (unbounded/infinite list metaphor), pure binary search on [0, n-1] is not applicable without knowing n.


3. The Core Idea

Zoom out exponentially until you pass the target (or hit the end), then zoom in with binary search on the last doubled interval. Like climbing stairs two steps at a time until you overshoot, then fine-tuning.


4. How It Works (Step-by-Step)

StepAction
1If first element is target, return 0.
2Set bound = 1; while bound < n and arr[bound] < target, bound *= 2.
3Binary search between bound/2 and min(bound, n-1).

5. Dry Run Example

Array: [1,2,3,5,8,13,21,34], target: 13.

Phaseboundarr[bound]Action
Double12< 13 → continue
Double23< 13
Double413≥ 13 → binary on [2, 4]

Binary finds index 5 (0-based: actually indices: bound/2=2 to min(4,7)=4 — arr[4]=13 → found).


6. Key Properties

PropertyDetail
TimeO(log i) if target at index i; O(log n) overall for finite n.
UseUnbounded search patterns; also first ≥ x style ranges.

7. Where It Is Used

Company / domainUse
Streaming / logsFind first record ≥ timestamp without full size.
SystemsBounded binary after coarse localization.

8. Interview Tips

Explain why double: bracket in logarithmic number of outward steps. Combine with binary for the inner phase. Watch off-by-one on bounds.


9. Comparison with Other Algorithms

AlgorithmNote
BinaryNeeds known hi upfront.
ExponentialFinds hi adaptively.
JumpFixed √n stride, not doubling.

10. Complexity

TimeSpace
O(log i) locate + O(log i) binaryO(1) iterative

Implementation Example (PYTHON)

def exponential_search(arr, target):
    n = len(arr)
    if n == 0:
        return -1
    if arr[0] == target:
        return 0
    bound = 1
    while bound < n and arr[bound] < target:
        bound *= 2
    lo = bound // 2
    hi = min(bound, n - 1)
    while lo <= hi:
        mid = (lo + hi) // 2
        if arr[mid] == target:
            return mid
        if arr[mid] < target:
            lo = mid + 1
        else:
            hi = mid - 1
    return -1

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer