Compute minimum required meeting rooms
Company: Apple
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of interval scheduling and resource-allocation concepts, proficiency with data structures for detecting temporal overlaps (heap and sweep-line paradigms), and the ability to analyze time and space complexity while handling edge cases.
Constraints
- 0 <= intervals.length <= 10^5
- intervals[i].length == 2
- 0 <= start_i <= end_i <= 10^9
- Intervals are half-open [start, end): a meeting ending at t does not conflict with one starting at t
- Zero-length meetings [t, t] are allowed
Examples
Input: [[0, 30], [5, 10], [15, 20]]
Expected Output: 2
Explanation: [0,30) overlaps both [5,10) and [15,20). [5,10) and [15,20) don't overlap each other, so they can share the second room sequentially. Peak concurrency = 2.
Input: [[7, 10], [2, 4]]
Expected Output: 1
Explanation: [2,4) ends before [7,10) starts, so the single room is reused. 1 room suffices.
Input: []
Expected Output: 0
Explanation: No meetings means no rooms are needed.
Input: [[1, 5]]
Expected Output: 1
Explanation: A single meeting needs exactly one room.
Input: [[1, 1], [1, 1], [1, 1]]
Expected Output: 1
Explanation: Zero-length meetings [1,1) occupy no time. With the half-open rule (end <= start), each frees its room instantly, so they all reuse one room. 1 room.
Input: [[0, 10], [10, 20], [20, 30]]
Expected Output: 1
Explanation: Back-to-back meetings: each ends exactly when the next starts. Half-open intervals mean no overlap, so one room is reused across all three.
Input: [[1, 10], [2, 7], [3, 19], [8, 12], [10, 20], [11, 30]]
Expected Output: 4
Explanation: At time just after 11, the meetings [3,19), [8,12), [10,20), [11,30) all overlap -> peak concurrency 4.
Input: [[5, 8], [6, 8]]
Expected Output: 2
Explanation: [5,8) and [6,8) overlap on [6,8) and even share the same end time 8, so they need separate rooms simultaneously. 2 rooms.
Hints
- Sort the meetings by start time so you process them in chronological order.
- Track only the END times of meetings currently occupying rooms; the earliest end time is all you need to decide whether a room frees up.
- A min-heap gives the earliest-freeing room in O(log n). If heap top <= current start, reuse that room (heapreplace); otherwise push a new room.
- The minimum number of rooms equals the peak number of simultaneously overlapping meetings, which is the maximum heap size — and since rooms are never removed once the peak is reached only by net additions, the final heap size equals that peak.