Data Structures · Visualization

Sparse Table: Range Query (RMQ)

Sparse Table

Once built, a sparse table answers a min/max range query by picking the largest power of two that fits within the range and combining two (possibly overlapping) precomputed ranges of that size — overlap is fine because min/max are idempotent. Time: O(1) per query after O(n log n) preprocessing. Space: O(1) per query.

Big-O: Build O(n log n); range-min query O(1).

Sparse Table: Range Query (RMQ)

Sparse Table

Once built, a sparse table answers a min/max range query by picking the largest power of two that fits within the range and combining two (possibly overlapping) precomputed ranges of that size — overlap is fine because min/max are idempotent.

How Range Query (RMQ) Works

  1. Given a query range [L, R], compute length = R - L + 1.
  2. Find k = floor(log2(length)), the largest power of two ≤ length.
  3. Look up table[k][L] (covers [L, L+2^k-1]) and table[k][R-2^k+1] (covers [R-2^k+1, R]).
  4. Combine the two values (e.g. take the min) — their union covers the full range, overlap doesn't affect the result.

Range Query (RMQ) Worked Example

Input: Query min of range [1,6] using the table built above

  1. length = 6, k = floor(log2(6)) = 2 (covers ranges of length 4)
  2. table[2][1] covers [1,4] → min = 2
  3. table[2][3] covers [3,6] → min = 1
  4. combine(2, 1) = 1

Output: Minimum of range [1,6] is 1, answered in O(1)

Range Minimum QueryO(1)
Answers any [l, r] min with two table lookups of the largest power-of-two block that fits, taking their min — O(1).
a[idx]
50
21
42
73
64
35
16
87
k=1
2
2
4
6
3
1
1
k=2
2
2
3
1
1
k=3
1
Reading pairwise mins · queried window Query blocks on k=-1
Pair / window being combined Query block Precomputed min
Range Minimum Query
Press ▶ Run to watch it step by step.
Python · Sparse Tables in Code
def range_min(st, l, r):        # inclusive query
    length = r - l + 1
    j = int(math.log2(length))
    span = 1 << j
    return min(st[j][l], st[j][r - span + 1])

# two overlapping blocks cover the range:
# O(1) per query after O(n log n) preprocessing
# example: range min query
Input: Query min of range [1,6] using the table built above

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Range Query (RMQ) Time & Space Complexity

TimeO(1) per query after O(n log n) preprocessing
SpaceO(1) per query

When to Use Range Query (RMQ)

  • Answering thousands of range-min queries on a static array
  • Competitive programming range queries
  • LCA queries in static trees

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.