Pattern 16 of 18 · Final Stretch

Intervals

Sort by start or end time, then sweep — the key to overlap and scheduling problems.

Problems about ranges — meeting times, event schedules, ranges on a number line — almost always start with sorting the intervals by start (or end) time, then sweeping through them once.

Key concepts

  • Sorting by start time groups intervals in the order they'd be encountered; sorting by end time helps with scheduling/selection problems.
  • Overlap check: two intervals [a, b] and [c, d] overlap exactly when a ≤ d and c ≤ b.
  • A sweep: walk through sorted intervals once, merging, counting, or tracking active intervals as you go.

When to use it

  • The problem talks about meetings, bookings, ranges, or scheduling.
  • You need to merge overlapping ranges, find gaps, or count how many overlap at once.

Tips

  • Sort first — nearly every interval problem becomes a straightforward single pass once the intervals are sorted.
  • For "minimum rooms/resources needed" problems, track start and end events separately, or use a min-heap of end times.
  • Draw a few intervals on a number line by hand — overlap conditions are much easier to get right visually than symbolically.

Practice problems (6)

Blind 75 (5):

More from Blind 150 (1):

See the full roadmap →