Compute minimum rooms for time intervals
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of interval overlap, resource allocation and the ability to implement efficient scheduling algorithms for meeting-room assignment.
Constraints
- 1 <= len(intervals) <= 10^5
- 0 <= start < end <= 10^9
- Intervals may be unsorted
Examples
Input: [[0, 30], [5, 10], [15, 20]]
Expected Output: 2
Explanation: The meeting [0,30) overlaps with both [5,10) and [15,20), but [5,10) and [15,20) do not overlap each other. At most 2 rooms are needed.
Input: [[1, 2]]
Expected Output: 1
Explanation: Only one meeting exists, so one room is enough.
Input: [[1, 4], [4, 5], [5, 7]]
Expected Output: 1
Explanation: Each meeting starts exactly when the previous one ends. Since end times are exclusive, they do not overlap, so a single room can be reused.
Input: [[2, 5], [2, 3], [2, 4], [3, 6]]
Expected Output: 3
Explanation: From time 2 to 3, three meetings are active: [2,5), [2,3), and [2,4). At time 3, [2,3) ends exactly as [3,6) starts, so the maximum remains 3.
Input: [[7, 10], [2, 4], [4, 7]]
Expected Output: 1
Explanation: Even though the intervals are unsorted, they form a chain of non-overlapping meetings: [2,4), [4,7), [7,10). One room is sufficient.
Hints
- Try sorting all start times and all end times separately, then sweep through time.
- If the next meeting starts at or after the earliest ending meeting, that room can be reused because intervals are [start, end).