Meeting Rooms: The Interval Warm Up That Still Filters
If you interview at Meta, Microsoft, Amazon, expect some version of "Meeting Rooms". It is rated Easy, and it falls squarely into the Merge Intervals pattern — pattern-first preparation beats grinding random problems every time. Part 4 of 11 in the Merge Intervals arc.
The Problem
Given an array of meeting time intervals, determine if a person could attend all meetings — that is, whether no two intervals overlap. A five-minute phone-screen warm-up that still filters out candidates who reach for pairwise comparison.
Input: intervals = [[0, 30], [5, 10], [15, 20]]
Output: False
[0, 30] collides with both others.
Recognizing the Merge Intervals Pattern
Interval problems — scheduling, calendars, resource booking — almost all reduce to one move: sort by start (or end), then sweep once while comparing each interval against a running boundary. Overlap exists when the next start is at most the current end. Sort plus linear sweep, O(n log n), is the backbone of the entire family.
After sorting by start, any conflict must appear between adjacent intervals — if interval i overlaps interval j beyond it, it necessarily overlaps i + 1 first. Adjacent-only checking is the entire insight; O(n²) pairwise comparison is the trap answer.
The Approach
Sort by start time, then scan once: if any interval starts before the previous one ends, return False. Back-to-back meetings (one ends at 10, the next starts at 10) are typically fine — confirm the convention with the interviewer, since it changes a single comparison operator.
Total: one sort, one comparison per adjacent pair. Say the adjacency argument in one sentence before writing anything.
Python Solution
def can_attend_meetings(intervals: list[list[int]]) -> bool:
"""True if no two meetings overlap (touching is allowed)."""
intervals.sort(key=lambda iv: iv[0])
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i - 1][1]:
return False
return True
Complexity
- Time: O(n log n) — dominated by the sort
- Space: O(1) — in-place sort, single boundary comparison
Interview Tips and Follow-Ups
- The real interview is the follow-up: 'how many rooms do you need?' — Meeting Rooms II, next in this arc. Treat this as its setup.
- State the touching convention explicitly; switching
<semantics silently is how easy questions get failed. - If intervals arrive as start/end object pairs or unsorted streams, mention sorting cost and whether a heap changes anything — bridge-building to II.
More Merge Intervals problems — and the other eight patterns — live in the Technical Interview category. Drill the pattern, not the problem: that is the entire thesis of this series.
Keep reading
Car Pooling: Difference Arrays Meet Interval Events
Capacity checking with +passengers/−passengers events on a number line. Python solution and complexity analysis for the merge intervals interview pattern.
Employee Free Time: Gaps in a Merged Union of Schedules
Common free intervals across k schedules — flatten, merge, read the gaps. Python solution and complexity analysis for the merge intervals interview pattern.
Insert Interval Without Re-Sorting Anything
Exploit existing sortedness: before, absorb, after — three phases. Python solution and complexity analysis for the merge intervals interview pattern.
Newsletter
New posts, straight to your inbox
One email per post. No spam, no tracking pixels, unsubscribe anytime.
Comments
- No comments yet. Be the first.