Pattern · Time O(n log n) for the sort; O(n) for the sweep.

Interval Sweep — the shape behind dozens of interview problems.

TL;DR

Sort events by time, sweep a line, maintain an active set for overlap questions. Time O(n log n) for the sort; O(n) for the sweep.

When to reach for Interval Sweep

  • Merge / intersect / count overlapping intervals.
  • Meeting-room count and scheduling conflicts.
  • Skyline and "maximum concurrent" queries.

Scenario variants (legal, original)

  • Meeting Room Budget — minimum rooms for overlapping meetings.
  • Event Merge — collapse overlapping intervals into a minimal set.
  • Skyline Silhouette — city skyline from rectangles.

Interval Sweep in Python

def min_rooms(intervals: list[tuple[int, int]]) -> int:
    events: list[tuple[int, int]] = []
    for s, e in intervals:
        events.append((s, 1))
        events.append((e, -1))
    events.sort()
    cur = best = 0
    for _, delta in events:
        cur += delta
        best = max(best, cur)
    return best

See language-specific tabs at /languages/python/patterns and the SQL variant at /languages/sql/patterns.

Common bugs

  • Closing events sorting before opening events at the same timestamp (flips inclusivity).
  • Confusing [start, end) with [start, end] semantics.
  • Using a heap when a sweep with sorted events is cleaner.

Related patterns

Frequently asked questions

What is the interval sweep pattern?
Interval Sweep is sort events by time, sweep a line, maintain an active set for overlap questions. Time O(n log n) for the sort; O(n) for the sweep.
When should I use interval sweep?
Use it when: Merge / intersect / count overlapping intervals. Meeting-room count and scheduling conflicts. Skyline and "maximum concurrent" queries.
What are the most common interval sweep bugs?
Closing events sorting before opening events at the same timestamp (flips inclusivity). Confusing [start, end) with [start, end] semantics. Using a heap when a sweep with sorted events is cleaner.
What problems use the interval sweep pattern?
Representative scenarios: Meeting Room Budget — minimum rooms for overlapping meetings. Event Merge — collapse overlapping intervals into a minimal set. Skyline Silhouette — city skyline from rectangles.
Is interval sweep on the Blind 75?
Yes — Interval Sweep is one of the core patterns behind the Blind 75 list. See /blind-75 for the complete mapping.

Run the free diagnostic.

Ten-minute patterns quiz. No card. Personalized loop starts on the other side.