17. Meeting Rooms
easyAsked at BookingDetermine if a person can attend all meetings — Booking applies the same interval-overlap check when validating that a property's booking windows never collide on the same room.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an array of meeting time intervals where intervals[i] = [start_i, end_i], determine if a person could attend all meetings (no two intervals overlap).
Constraints
0 <= intervals.length <= 10^4intervals[i].length == 20 <= start_i < end_i <= 10^6
Examples
Example 1
intervals = [[0,30],[5,10],[15,20]]falseExplanation: [0,30] overlaps with both [5,10] and [15,20].
Example 2
intervals = [[7,10],[2,4]]trueApproaches
1. Brute force
Check every pair of intervals for overlap.
- Time
- O(n^2)
- Space
- O(1)
function canAttendMeetings(intervals) {
for (let i = 0; i < intervals.length; i++) {
for (let j = i + 1; j < intervals.length; j++) {
const [s1, e1] = intervals[i];
const [s2, e2] = intervals[j];
if (s1 < e2 && s2 < e1) return false;
}
}
return true;
}Tradeoff:
2. Sort then linear scan
Sort by start time; overlap exists only if a start falls before the previous end. Single pass after sort.
- Time
- O(n log n)
- Space
- O(1)
function canAttendMeetings(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < intervals[i - 1][1]) return false;
}
return true;
}Tradeoff:
Booking-specific tips
Booking cares that you immediately reach for sorting by start time — it mirrors how their calendar engine validates reservation windows. State the overlap condition (s2 < e1) precisely; off-by-one on boundary equality trips many candidates.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Meeting Rooms and other Booking interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →