Skip to main content
Back to all problems
Beginner guide, Algorithms & data structures

Intervals

Sort by start, then handle overlaps in a single pass.

An interval is a range with a start and an end, written [start, end]: a meeting from 9 to 11, a booking from day 3 to day 7, the numbers from 2 to 6. Interval problems ask how these ranges overlap, and what to do about it: merge them, count them, or remove some.

When do two intervals overlap?

Two intervals [a1, a2] and [b1, b2] overlap when each one starts before the other ends:

text
a1 < b2  and  b1 < a2
text
overlap:        [a1=====a2]
                     [b1=====b2]

no overlap:     [a1==a2]
                            [b1==b2]

touching:       [a1=====a2]
                          [b1=====b2]     (a2 === b1)

Touching (one ends exactly when the next starts) is the case to watch. Some problems treat it as an overlap (Merge Intervals, Insert Interval), others don't (Meeting Rooms). Read the problem carefully: it decides whether you write < or <=.

The first move: sort

Almost every interval problem starts by sorting. Once the intervals are in order, each one only needs to be compared with its neighbor, instead of with every other interval: O(n log n) instead of O(n²).

But sort by what?

Sort byUse it whenProblems
startyou're merging or checking neighbors for clashesMeeting Rooms, Merge Intervals
endyou want to keep as many non-overlapping intervals as possibleNon-overlapping Intervals
(already sorted)the input is given sorted: skip the sort, do it in O(n)Insert Interval

Pattern 1: sort by start, then sweep

Walk through the sorted intervals, comparing each with the last one you kept (or the current merged group):

ts
sorted.sort((a, b) => a[0] - b[0])
for (const [start, end] of sorted) {
  const last = result.at(-1)
  if (last && start <= last[1])
    last[1] = Math.max(last[1], end) // overlaps: extend
  else result.push([start, end]) // a gap: new group
}

Note the max: an interval completely inside the group (like [2, 3] inside [1, 10]) must not shrink it.

Pattern 2: earliest end first (greedy)

To keep the most non-overlapping intervals, always keep the one that ends earliest: it leaves the most room for the rest. Sort by end, keep each interval that starts after the last kept one ends. (See the Greedy primer for why this kind of choice is safe.)

Pattern 3: sweep line (count overlaps at each moment)

To find how many intervals overlap at the busiest moment, turn every interval into two events: +1 at its start, −1 at its end. Walk through the events in time order and track the running count. The highest count is the answer.

This answers Meeting Rooms II (rooms needed = most meetings at once). When a start and an end happen at the same time, decide which goes first based on whether touching counts as overlapping.

How to recognize an interval problem

  • The input is a list of pairs [start, end]: times, ranges, bookings.
  • The question mentions overlap, merge, conflict, schedule, rooms, or free time.

Common mistakes

  • Forgetting to sort (or sorting by the wrong end).
  • Getting touching wrong: < vs <=. Check what the problem says about intervals that share an endpoint.
  • Overwriting the end instead of taking the max when merging.
  • Mutating the input while sorting or merging, when the caller still uses it. Sort a copy ([...intervals].sort(...)).

Practice what you learned

The problems go from easier to harder. Try each one on your own and open a hint when you get stuck.