Skip to main content

Interval Problems

A common pattern in interviews involving ranges represented as [start, end] pairs.

Key Technique: Sort First

Almost every interval problem starts with sorting by start time (or sometimes end time).

Core Patterns

1. Merge Intervals

Combine overlapping intervals:

2. Insert Interval

Insert a new interval and merge if necessary — handle three cases: before, overlapping, after.

3. Interval Intersection

Find common overlaps between two sorted lists of intervals using two pointers.

4. Meeting Rooms (Overlap Check)

  • Can attend all? — sort by start, check for any overlap
  • Min rooms needed? — use a min-heap or sweep line on start/end events

5. Sweep Line

Process events (start/end) in sorted order to track active intervals:

Complexity

Classic Problems

  • Merge Intervals
  • Insert Interval
  • Non-overlapping Intervals (min removals)
  • Meeting Rooms I & II
  • Minimum Number of Arrows to Burst Balloons
  • Interval List Intersections
  • Employee Free Time
  • My Calendar I, II, III

When to Use

  • Problems involving time ranges, schedules, or bookings
  • Overlapping or conflicting range detection
  • Resource allocation (rooms, CPUs, etc.)
  • Any problem with [start, end] pair inputs