Meeting Rooms
Problem Statement
Given an array of meeting time intervals, determine whether one person can attend every meeting. If one meeting ends exactly when another begins, the person can attend both because there is no time overlap.
Input
An integer matrix intervals, where intervals[i] = [starti, endi] represents one meeting.
Output
A boolean: true if all meetings can be attended, otherwise false.
Constraints
- •
0 <= intervals.length <= 10^4 - •
0 <= starti < endi <= 10^6
Examples
Example 1
intervals = [[0,30],[5,10],[15,20]]
falseExample 2
intervals = [[7,10],[2,4]]
trueLearning Objectives
- Recognise meeting attendance as a pure overlap-detection problem.
- Sort intervals by start time so any conflict appears between adjacent intervals.
- Apply the correct half-open meeting rule where **end <= start** is safe.
- Explain why a boolean scheduling question does not need a heap or room count.
Intuition
Pattern Recognition
The signal is can attend all meetings, which means the question is not asking for a schedule or a room count. It only asks whether any two intervals overlap. The trap is comparing every pair or treating a meeting that ends at time t as conflicting with one that starts at time t.
Sort by start time and the calendar becomes a left-to-right scan. If the current meeting starts before the previous meeting ends, those two meetings overlap and the answer is immediately false. If every adjacent pair is compatible, then no hidden non-adjacent conflict can survive the sorted order without creating an adjacent conflict on the way.
Common mistakes
- ×Using **current start <= previous end**, which incorrectly rejects back-to-back meetings.
- ×Sorting by end time and then comparing neighbours, which does not preserve the order meetings are requested.
- ×Building a heap even though the problem only asks whether one person can attend all meetings.
- ×Continuing the scan after finding an overlap instead of returning **false** immediately.
Algorithm Explanation
Key idea
Sort intervals by start time. In that order, the only meeting that can first expose a conflict with the current meeting is the one immediately before it. The overlap rule is current start < previous end. Equality is allowed because the previous meeting has already ended.
Interval walkthrough
Use intervals = [[0,30],[5,10],[15,20]]. On the number line, [0,30] stretches across the entire window. The next meeting [5,10] begins inside that occupied span because 5 < 30. That single adjacent comparison proves the person is double-booked. For [[2,4],[7,10]], the carried previous end is 4, and the next start 7 is to the right of it, so the meetings are compatible.
Algorithm
- Sort intervals by increasing start time.
- Start at the second interval because the first interval has no previous neighbour.
- Compare the current start with the previous end.
- If current start < previous end, return false because the meetings overlap.
- If the scan finishes without finding an overlap, return true.
Solutions
Solution: Sort by start and check adjacent intervals
Use this as the canonical solution when all intervals are available up front and the question is only whether one attendee has a conflict.
Sort meetings by start time, then compare each meeting with the meeting immediately before it. Once starts are ordered, any overlap must be visible as an adjacent pair where the later start is still before the earlier end.
Step-by-step
- Sort intervals by the first coordinate.
- Iterate from index 1 to the end.
- Let the previous meeting be intervals[index - 1] and the current meeting be intervals[index].
- If the current start is less than the previous end, return false.
- Return true after all adjacent pairs pass the check.
O(n log n)
O(n)
Sorting dominates the runtime. In Java, sorting an array with a comparator may use auxiliary storage; the scan itself uses constant extra space.
Java implementation
Dry Run
Sample input
intervals = [[0,30],[5,10],[15,20]]. After sorting, the order is unchanged.
| step | previous meeting | current meeting | decision |
|---|---|---|---|
| 1 | [0,30] | [5,10] | 5 starts before 30, so return false |
The first adjacent comparison already finds a conflict. There is no need to inspect the remaining meeting because one overlap makes the answer false.
Interview Tips
Lead with the distinction between Meeting Rooms I and II. This problem is a boolean conflict check, so sorting plus adjacent comparisons is enough. Say the overlap rule precisely: start < previous end conflicts, while start >= previous end is safe. That equality detail is a common interviewer edge case.
Likely follow-ups
- How would you return the first conflicting pair instead of a boolean?
- How would the solution change if the meetings arrived one at a time online?
- How would you count the minimum number of rooms instead of checking one attendee?
- How would you handle meetings with inclusive end times where touching endpoints do overlap?
Similar Problems
Key Takeaways
- Sort by start time before checking interval conflicts.
- Adjacent comparisons are enough for a boolean overlap question.
- **end == start** means two meetings can be attended back to back.
- Meeting Rooms I does not require tracking active rooms.