Assign Meetings to the Minimum Number of Rooms
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Implement `assign_meeting_rooms(intervals)`. Each interval is `[start, end)`, so a room used by a meeting ending at time `t` is available to another meeting starting at `t`.
Return one flat integer list `[room_count, room_for_meeting_0, room_for_meeting_1, ...]`. The first value is the minimum number of rooms required, and each following value is the nonnegative room ID assigned to the interval at that original input index.
The output must be deterministic:
- Process meetings by increasing start time, then increasing end time, then original index.
- Reuse the smallest room ID that is free at the meeting's start.
- If no room is free, allocate the next unused room ID.
Every interval has `start < end`.
```hint Track two priorities
One priority queue can identify which occupied room becomes free next, while a second can return the smallest currently available room ID.
```
```hint Free all eligible rooms
Before assigning a meeting, release every room whose previous meeting has already ended, not only the earliest one.
```
### Discussion Extensions
- If only the minimum room count were required, how would a sweep line solve the problem?
- What changes if intervals arrive as a stream and cannot first be globally sorted?
Quick Answer: Assign meetings to the minimum number of rooms while returning a deterministic room ID for every original interval. Use priority queues to release all eligible rooms, reuse the smallest free ID, and preserve half-open interval semantics.
Implement assign_meeting_rooms(intervals) for half-open meetings [start, end). Return [room_count, room_for_meeting_0, room_for_meeting_1, ...]. Process meetings by start, then end, then original index; release every room whose meeting ended by the next start; reuse the smallest free room ID, or allocate the next unused ID.
Constraints
- 0 <= intervals.length <= 20.
- Every interval is [start, end) with integer start < end.
- Each time is between -3,000,000,000 and 3,000,000,000.
- Meetings are processed by start time, then end time, then original index.
- Every room ending at or before a meeting's start is free, and the smallest free room ID must be reused.
Examples
Input: []
Expected Output: [0]
Explanation: No meetings require zero rooms and no assignments.
Input: [[0, 10]]
Expected Output: [1, 0]
Explanation: One meeting receives room 0.
Hints
- Use one min-heap ordered by end time to discover all rooms that have become free.
- Put every released room ID into a second min-heap so reuse is deterministic.
- Store assignments by original interval index even though processing uses a sorted order.