Simulate meeting-room bookings and return busiest room
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's competence in scheduling and resource-allocation algorithms, focusing on simulation of interval-based bookings, tie-breaking logic, and tracking per-resource assignment counts.
Constraints
- `1 <= n <= 10^5`
- `1 <= len(meetings) <= 2 * 10^5`
- Each meeting satisfies `0 <= start < end <= 2^31 - 1`
- Delayed meeting end times may exceed the original input time range
Examples
Input: (2, [[0, 10], [1, 5], [2, 7], [3, 4]])
Expected Output: 0
Explanation: Room 0 gets meetings [0,10] and delayed [10,11]. Room 1 gets [1,5] and delayed [5,10]. Both handle 2 meetings, so return the smaller room id 0.
Input: (3, [[1, 20], [2, 10], [3, 5], [4, 9], [6, 8]])
Expected Output: 1
Explanation: Two meetings must be delayed. Final counts are room 0 -> 1, room 1 -> 2, room 2 -> 2. Rooms 1 and 2 tie, so return 1.
Input: (1, [[5, 7], [1, 2], [2, 3], [3, 4]])
Expected Output: 0
Explanation: There is only one room, so every meeting goes to room 0. Meetings at exact end times can start immediately, so room 0 handles all 4 meetings.
Input: (3, [[0, 5], [0, 5], [0, 5], [5, 10], [5, 10], [5, 10]])
Expected Output: 0
Explanation: The first three meetings use rooms 0, 1, and 2. At time 5 all rooms become free, and the next three meetings again use rooms 0, 1, and 2. Each room handles 2 meetings, so return 0.
Input: (3, [[5, 6], [0, 100], [2, 3], [4, 5], [1, 2], [3, 4]])
Expected Output: 1
Explanation: After sorting, room 0 stays busy with [0,100]. Room 1 repeatedly becomes the smallest available room for the short meetings, so it handles 5 meetings in total and is the busiest.
Hints
- Use one min-heap for currently free rooms so you can always pick the smallest room id quickly.
- Use another min-heap for busy rooms ordered by `(end_time, room_id)`. Before processing a meeting, release every room whose meeting has already ended.