Compute minimum required meeting rooms
Company: Apple
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: 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.
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.