Data Structures · Visualization

Interval Tree: Overlap Query

Interval Tree

Two ranges overlap when each starts before the other ends: low1 ≤ high2 AND low2 ≤ high1. Time: O(log n + k) for k reported overlaps. Space: O(1) extra.

Big-O: Insert O(log n); overlap query O(log n + k).

Interval Tree: Overlap Query

Interval Tree

Two ranges overlap when each starts before the other ends: low1 ≤ high2 AND low2 ≤ high1. The search checks the current node, then uses maxHigh to decide which children could possibly overlap — a child whose subtree ends before the query starts is skipped entirely, which is where the speed comes from.

How Overlap Query Works

  1. If the current interval overlaps the query, collect it.
  2. If the left child exists and left.maxHigh ≥ query.low, search left.
  3. Otherwise (or additionally, when intervals can span), search right.
  4. Repeat until pruned branches stop the walk — each reported overlap costs O(1).

Overlap Query Worked Example

Input: Query [4, 7] against [1, 3], [5, 8], [2, 6], [9, 12]

  1. [2, 6] vs [4, 7]: 2 ≤ 7 and 4 ≤ 6 → overlap, collect.
  2. Left maxHigh 3 < 4 → skip the whole left branch.
  3. [5, 8] vs [4, 7] → overlap, collect.
  4. [9, 12]: 9 > 7 → no overlap, stop.

Output: 2 overlaps found, 1 branch pruned, 1 interval rejected

Interval Tree: Introduction
Ranges with a maxHigh shortcut per subtree for overlap pruning.
[1, 3]
[5, 8]
[2, 6]
Visiting Overlaps query
Interval Tree: Introduction
Press ▶ Run to watch it step by step.
Python · Interval Tree in Code
def overlaps(a, b):
    return a[0] <= b[1] and b[0] <= a[1]

def query(node, q, out):
    if not node: return
    if overlaps(node['iv'], q): out.append(node['iv'])
    if node.get('left') and node['left']['max'] >= q[0]:
        query(node['left'], q, out)   # left may overlap
    # else: whole branch pruned
    if node.get('right') and node['iv'][0] <= q[1]:
        query(node['right'], q, out)  # O(log n + k)
# example: query
Input: Query [4, 7] against [1, 3], [5, 8], [2, 6], [9, 12]

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.

Overlap Query Time & Space Complexity

TimeO(log n + k) for k reported overlaps
SpaceO(1) extra

When to Use Overlap Query

  • 'What meetings clash with 2–4pm?' in scheduling UIs
  • Collision broad-phase: which objects touch this region?

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.