Find Minimum Rooms Needed
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of interval scheduling and conflict detection in time ranges, along with algorithmic reasoning and appropriate data structure use for managing concurrent events and resource allocation.
Part 1: Minimum Number of Meeting Rooms
Constraints
- 0 <= n <= 200000, where n is the number of meetings
- -10^9 <= start < end <= 10^9
- Intervals are half-open: [start, end), so end == next_start does not overlap
Examples
Input: [[0, 30], [5, 10], [15, 20]]
Expected Output: 2
Explanation: The meeting [0, 30) overlaps with both later meetings, so two rooms are needed.
Input: [[1, 3], [3, 5], [5, 7]]
Expected Output: 1
Explanation: Because intervals are half-open, each meeting starts exactly when the previous one ends, so one room is enough.
Input: []
Expected Output: 0
Explanation: No meetings means no rooms are required.
Input: [[7, 10], [2, 4]]
Expected Output: 1
Explanation: These meetings do not overlap, even though the input is unsorted.
Input: [[1, 5], [2, 6], [4, 8], [5, 7]]
Expected Output: 3
Explanation: At time 4, three meetings are active: [1, 5), [2, 6), and [4, 8).
Hints
- Sort meetings by start time. As you scan from left to right, keep track of meetings that are still in progress.
- A min-heap of end times lets you quickly find the room that becomes free the earliest.
Part 2: Produce a Valid Meeting Room Assignment
Constraints
- 0 <= n <= 200000, where n is the number of meetings
- -10^9 <= start < end <= 10^9
- Intervals are half-open: [start, end), so end == next_start does not overlap
- Room numbers must start at 0 and use the smallest available room under the stated rules
Examples
Input: [[0, 30], [5, 10], [15, 20]]
Expected Output: [0, 1, 1]
Explanation: Meeting 0 gets room 0. Meeting 1 overlaps with it, so it gets room 1. Meeting 2 can reuse room 1 after meeting 1 ends.
Input: [[1, 3], [3, 5], [5, 8]]
Expected Output: [0, 0, 0]
Explanation: Each meeting starts exactly when the previous one ends, so the same room can be reused.
Input: []
Expected Output: []
Explanation: No meetings means no assignments.
Input: [[5, 10], [0, 30], [15, 20], [30, 40]]
Expected Output: [1, 0, 1, 0]
Explanation: After sorting by start time, room 0 is used by [0, 30), room 1 by [5, 10), room 1 is reused for [15, 20), and room 0 is reused for [30, 40).
Input: [[1, 4], [1, 3], [1, 2]]
Expected Output: [0, 1, 2]
Explanation: All meetings start at the same time, so none can share a room. They are processed in original index order.
Hints
- Use one min-heap for active rooms keyed by meeting end time, and another min-heap for room numbers that are currently free.
- Sort meetings by (start time, original index) so that the output is deterministic.