HARD Lab · Intervals · 51

252. Meeting Rooms

Given a list of meeting time intervals, determine whether a person can attend all of them — i.e., no two meetings overlap. Sort by start time, then a single pass checking adjacent pairs is all it takes.

EasySortingIntervalsTypeScript

PROBLEM What we're solving

You are given a list of meeting intervals, each as [start, end]. Return true if a person can attend every meeting without any two overlapping. For example, [[0,30],[5,10],[15,20]]false because [0,30] and [5,10] collide. But [[7,10],[2,4]]true — after sorting, [2,4] ends before [7,10] starts.

KEY IDEA Sort by start → only adjacent pairs can conflict

Insight → once intervals are sorted by start time, the only pairs that can overlap are consecutive ones. If meeting i ends after meeting i+1 starts (prev.end > curr.start), there's a conflict. You never need to compare non-adjacent meetings — sorting collapses O(n²) checks to O(n).

RECIPE Sort, then scan adjacent pairs

  • 1 · Sort by start time. Brings potentially-overlapping meetings next to each other — a conflict can only happen between neighbours.
  • 2 · Walk from index 1 to n-1. Compare intervals[i-1][1] (end of previous) with intervals[i][0] (start of current).
  • 3 · If prev.end > curr.start, they overlap — return false immediately.
  • 4 · Reach the end without a conflict → return true.
Classic confusion → the overlap condition is prev.end > curr.start (strict greater-than), not >=. Back-to-back meetings like [0,10],[10,20] are fine — one ends exactly when the next starts, which is not an overlap. Using >= incorrectly rejects them.

COST Complexity & alternatives

Brute force (every pair)
O(n²)
Compare all pairs; no sorting needed, but quadratic.
Sort + linear scan
O(n log n)
Sort dominates; scan is O(n). O(1) extra space.

The sort is in-place so space is O(1)auxiliary (ignoring the recursion stack of the sort). You can't beat O(n log n) here because you need to distinguish any ordering of intervals — information-theoretic lower bound matches the sort.

Pattern transfer → this sort-then-scan skeleton powers Merge Intervals (merge overlapping instead of rejecting), Insert Interval (find the right sorted position), Meeting Rooms II (count overlapping meetings with a min-heap or two sorted arrays), and Non-Overlapping Intervals (greedy removal count).

RUN IT Sort by start, check adjacent pairs for overlap

step 0 / 2
STARTSort 3 meetings by start time. Sorted order lets us compare only adjacent pairs.
sorted meetings0-3005-10115-202
State
i
prev.end
curr.start
current pair under comparisonconflict — meetings overlapall clear — no conflicts
slowfast

TYPESCRIPT The solution, annotated

canAttendMeetings.ts
function canAttendMeetings(intervals: number[][]): boolean {
  intervals.sort((a, b) => a[0] - b[0]);

  for (let i = 1; i < intervals.length; i++) {
    const prev = intervals[i - 1];
    const curr = intervals[i];
    if (prev[1] > curr[0]) return false;  // overlap: prev ends after curr starts
  }

  return true;
}

Reading it block by block

Line 2 — sort by start. intervals.sort((a, b) => a[0] - b[0])puts meetings in chronological order. After this, any overlap must involve adjacent meetings — that's the key invariant the rest of the algorithm relies on.
Lines 4–8 — linear scan. Starting at index 1, compare prev[1] (the end of the meeting we just passed) against curr[0](the start of the next). If the previous meeting hasn't ended yet when the next one begins, they conflict.
Line 6 — the overlap test. prev[1] > curr[0] is the only check needed. Strict > because touching boundaries (e.g. 10 and 10) are allowed — the person walks out of one room and into the next.
Line 10 — clean exit. We checked every adjacent pair without finding a conflict, so the person can attend all meetings. Return true.
Complexity → O(n log n) time (sort) + O(n) (one scan) = O(n log n) overall. Space is O(1) auxiliary — the sort is in-place and the scan uses no extra structure.

INTERVIEWFollow-ups they'll ask

  • "Now return the minimum number of rooms needed (Meeting Rooms II)?" Sort start and end times separately and use two pointers (or a min-heap of end times) to count simultaneous meetings.
  • "What if intervals are given as objects instead of arrays?" The algorithm is identical — just change the comparator and accessor (a.start instead of a[0]).
  • "What if the person needs a travel buffer between rooms?" Change the condition to prev.end + buffer > curr.start.
  • "Can you do it without sorting?" Yes — O(n²) brute force checks every pair. Sometimes acceptable for tiny n, but sorting is universally preferred.
  • "Edge cases?" Empty input returns true (vacuously no conflicts). A single meeting returns true. All meetings identical returns false.

MNEMONIC The one-liner

"Sort the schedule, then walk it — if yesterday&apos;s meeting bleeds into today&apos;s, you&apos;re double-booked."

TRIGGERS When you see ___ → reach for ___

"can attend all meetings" / "no overlap"sort by start + adjacent-pair scan
detect overlap between intervalsprev.end > curr.start (strict)
"how many rooms needed"Meeting Rooms II — min-heap of end times
"merge / insert / remove intervals"same sort-then-sweep skeleton

SKELETON The reusable shape

skeleton.ts
function canAttendMeetings(intervals: number[][]): boolean {
  intervals.sort((a, b) => a[0] - b[0]);

  for (let i = 1; i < intervals.length; i++) {
    const prev = intervals[i - 1];
    const curr = intervals[i];
    if (prev[1] > curr[0]) return false;
  }

  return true;
}

FLASHCARDS Tap to flip

Why sort by start time?
Only adjacent meetings in sorted order can overlap — it reduces O(n²) pair checks to one O(n) pass.
tap to flip
1 / 6
Answer to reveal explanations. No penalty for retries.Score: 0 / 7
QUESTION 01
What is the time complexity of the optimal solution?
QUESTION 02
After sorting, what condition signals an overlap between prev = [s1, e1] and curr = [s2, e2]?
QUESTION 03
Trace [[0,30],[5,10],[15,20]]. After sorting by start, which pair triggers the overlap?
QUESTION 04
Does [[7,10],[2,4]] return true or false?
QUESTION 05
Why do we only compare adjacent pairs after sorting, rather than all pairs?
QUESTION 06
What is the space complexity of the sort-then-scan approach?
QUESTION 07
What change is needed to solve Meeting Rooms II (minimum rooms required)?