Skip to content
DSA Patterns
Menu
Language

Pattern #12

Intervals

Essential

Sort by start or end, then merge, insert, or remove overlaps.

When to use

Problems give ranges [start, end] and ask merge, coverage, min removals, or room counts.

Recognition cues

  • Merge intervals / insert interval
  • Meeting rooms / min arrows
  • Sort by start or by end

Common pitfalls

  • Inclusive vs exclusive ends
  • Sorting by the wrong key for the proof (end vs start)
  • Not handling empty or single-interval inputs

90-second recognition drill

Which pattern fits best?

  • Merge intervals / insert interval
  • Meeting rooms / min arrows
  • Sort by start or by end

Interactive

Mental model

A full worked walkthrough of the invariant. Pause, scrub the dots, or use ← →. Aim to narrate each step yourself.

Step 1 of 8
1/8

Unsorted intervals. Sort by start first.

[1,3]
[8,10]
[2,6]
[15,18]
Statestep=sort by start
Key:CurrentActive rangeMatchSettledEliminated

How to think about it

Sort creates order. Then a linear scan either merges overlapping ranges or applies a greedy rule (e.g. always keep the interval that finishes first). Drawing on a line makes overlaps obvious.

Template shapes

Shape Core move Example
Merge Sort by start. When next.start ≤ last.end, extend last.end. Ends that touch are an overlap. Otherwise append LC 56
Insert The input is already sorted. Merge the one new interval with that same touch-is-overlap rule, and copy the intervals that miss it LC 57
Can attend Sort by start. If next.start < prev.end, the person cannot attend both. Ends that only touch are still free LC 252
Min rooms Two sorted arrays, starts and ends. A start before the next end opens a room. An end at or before the next start closes one. The peak open count is the answer LC 253
Min removals Sort by end. If the next start is strictly before keptEnd, discard it. Otherwise that end becomes keptEnd LC 435
Two lists Both lists are sorted and disjoint inside themselves. The overlap is [max(starts), min(ends)] when that span is non-empty. Advance the list that ends sooner LC 986

Complexity baseline

O(n log n) for the sort, then O(n) scan. Space O(n) for output or sort. LC 57 and LC 986 skip the sort because the input is already ordered.

From template to problem

  1. Sort by start when the job is merge or a conflict scan (LC 56, LC 252). Sort by end when the job is “keep the one that finishes first” (LC 435).
  2. Touching ends overlap for merge (LC 56) and do not conflict for attendance (LC 252). Use ≤ or < to match that.
  3. Meeting rooms II does not merge. It walks a start pointer and an end pointer and tracks how many rooms are open (LC 253).
  4. On two already-sorted lists, advance the interval that ends sooner. On a tie, advance the second list (LC 986).

Template

Same skeleton in TypeScript, Python, and C#. Adapt the invariant; keep the structure.

Intervals · Template
/** Intervals template: merge overlapping ranges after sorting by start. */
export function merge(intervals: number[][]): number[][] {
  if (!intervals.length) return [];
  intervals = [...intervals].sort((a, b) => a[0]! - b[0]!);
  const out: number[][] = [[...intervals[0]!]];
  for (let i = 1; i < intervals.length; i++) {
    const cur = intervals[i]!;
    const last = out[out.length - 1]!;
    if (cur[0]! <= last[1]!) last[1] = Math.max(last[1]!, cur[1]!);
    else out.push([...cur]);
  }
  return out;
}
/** Intervals template: merge overlapping ranges after sorting by start. */
export function merge(intervals: number[][]): number[][] {
  if (!intervals.length) return [];
  intervals = [...intervals].sort((a, b) => a[0]! - b[0]!);
  const out: number[][] = [[...intervals[0]!]];
  for (let i = 1; i < intervals.length; i++) {
    const cur = intervals[i]!;
    const last = out[out.length - 1]!;
    if (cur[0]! <= last[1]!) last[1] = Math.max(last[1]!, cur[1]!);
    else out.push([...cur]);
  }
  return out;
}
#StatusProblemTypeDone
  1. 1#56 Merge IntervalsGuide
  2. 2#57 Insert IntervalGuide
  3. 3#252 Meeting RoomsGuide
  4. 4#253 Meeting Rooms IIGuide
  5. 5#435 Non-overlapping IntervalsGuide
  6. 6#986 Interval List IntersectionsGuide