Assign Meetings to Rooms and Find the Most-Used One
Heaps, Sorting & SearchModel answerLast updated
Simulate meetings being placed into n numbered rooms, delaying them when all rooms are full, then report the room that hosted the most meetings, preferring the lowest index on ties.
Room Allocation Simulation
An office has n meeting rooms numbered 0 to n-1, all empty at the start. You receive a list meetings, where each entry [start, end] is a request to hold a meeting from start (inclusive) to end (exclusive).
Meetings are placed with these rules:
- When a meeting is due to start and at least one room is empty, it takes the empty room with the smallest index.
- If every room is occupied, the meeting waits and takes the first room that becomes empty. A delayed meeting keeps its original duration (
end - start), so it ends later than requested. - If several meetings are waiting at once, the one with the earlier original start time gets the next free room.
- Meetings with identical start times are handled in the order they appear in the input.
- A room whose meeting ends at time
tcan host another meeting starting att.
Return the index of the room that hosted the most meetings. If several rooms tie, return the smallest index.
def busiest_room(n: int, meetings: List[List[int]]) -> int:
Example
n = 2, meetings = [[0, 10], [1, 5], [2, 7], [3, 4]]
[0,10]goes to room 0 and runs[0,10).[1,5]goes to room 1 and runs[1,5).[2,7]finds both rooms busy and waits until time 5, when room 1 frees up. Its duration is 5, so it runs[5,10)in room 1.[3,4]waits until time 10, when rooms 0 and 1 are both free. It takes room 0 and runs[10,11).
Room 0 hosted 2 meetings and room 1 hosted 2, so the answer is 0.
Constraints
1 <= n <= 50,0001 <= meetings.length <= 100,0000 <= start < end <= 10^9
What to cover in your answer
Explain your approach, justify its correctness, give time and space complexity, mention the edge cases (everything fits in one room, more meetings than rooms, back-to-back meetings), and provide code.
Hints
Hint 1
Handle meetings in order of start time. A stable sort keeps equal-start meetings in input order, which takes care of the tie rule.
Hint 2
Track two groups of rooms: a min-heap of free room indices, and a min-heap of (end_time, room_index) for occupied rooms. Before placing each meeting, move every room with end_time <= start from the busy heap to the free heap.
Hint 3
If no room is free, pop the busy heap's smallest entry. The meeting starts at that end time and lasts (end - start), and the tuple ordering already breaks ties toward the lower room index. Keep a per-room counter and scan it at the end.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach: sort, then simulate with two heaps
A note on the example
Because a delayed meeting keeps its duration, [2,7] cannot simply "end at 7" after starting at 5. It runs [5,10). Following that through gives room counts of 2 and 2, so the answer is 0. State this assumption early with the interviewer. It is the standard reading of the problem.
Algorithm
- Sort the meetings by start time using a stable sort. Meetings with equal starts then remain in input order.
- Maintain:
free: a min-heap of indices of idle rooms, initially0..n-1.busy: a min-heap of(end_time, room)for occupied rooms.count[room]: how many meetings each room has hosted.
- For each meeting
(s, e)in sorted order:- Release rooms. While the smallest end time in
busyis<= s, pop it and push its room intofree. - A room is free. Pop the smallest index from
freeand schedule(e, room)inbusy. - No room is free. Pop
(t, room)frombusy. This is the room that frees earliest, and ties go to the lower index because tuples compare element by element. The meeting runs fromttot + (e - s). Push(t + e - s, room). - Increment
count[room].
- Release rooms. While the smallest end time in
- Return the first index with the maximum count, scanning from 0 and updating only on a strictly larger count.
Why it works
- Queue order for free. Meetings are processed in start order, and delayed meetings receive non-decreasing start times. So the "earliest waiting meeting gets the next room" rule is satisfied without an explicit waiting queue.
- Free case. After the release step,
freecontains exactly the rooms idle at times. Its minimum is the lowest-index idle room. - Wait case. If
freeis empty, every room is occupied pasts. The smallest end timetis the first moment any room opens. If several rooms open att,(t, room)ordering selects the lowest index among them. That matches the rule that the lowest index wins when several rooms are free.
Complexity
Let m be the number of meetings.
- Time:
O(m log m + m log n + n). Sorting costsO(m log m). Each meeting causes a bounded number of pushes and pops, plus releases that are amortized against earlier pushes, each costingO(log n). Building the initial free heap isO(n). - Space:
O(n + m)for the heaps, counters, and sorted copy.
Code
import heapq
from typing import List
def busiest_room(n: int, meetings: List[List[int]]) -> int:
# Python's sort is stable, so equal starts keep input order.
order = sorted(meetings, key=lambda m: m[0])
free = list(range(n)) # sorted list is already a valid min-heap
busy = [] # (end_time, room)
count = [0] * n
for start, end in order:
# Free every room whose meeting has finished by `start`.
while busy and busy[0][0] <= start:
_, r = heapq.heappop(busy)
heapq.heappush(free, r)
if free:
r = heapq.heappop(free)
heapq.heappush(busy, (end, r))
else:
t, r = heapq.heappop(busy)
heapq.heappush(busy, (t + (end - start), r))
count[r] += 1
best = 0
for i in range(1, n):
if count[i] > count[best]:
best = i
return best
Trace of the example
| Meeting | State when it arrives | Room | Runs |
|---|---|---|---|
[0,10] | both rooms free | 0 | [0,10) |
[1,5] | room 1 free | 1 | [1,5) |
[2,7] | none free, room 1 frees at 5 | 1 | [5,10) |
[3,4] | none free, rooms 0 and 1 both free at 10 | 0 | [10,11) |
Counts are [2, 2], so the result is 0.
Edge cases
- Everything fits in one room. Room 0 is released before each meeting and always has the lowest index, so the answer is 0.
- Back-to-back meetings. The
<=in the release step lets a meeting starting attreuse a room that ends att. - Large end times. Accumulated delays can push end times far beyond
10^9. Python handles this natively, but Java or C++ need 64-bit integers. n = 1. Every meeting goes to room 0, possibly after a delay.- More rooms than meetings. Many rooms are never used, and the tie-break scan handles that naturally.
Common mistakes
- Releasing only one finished room instead of all rooms with
end <= start, which can hand out a higher-index room than allowed. - Using
<instead of<=when releasing, which delays back-to-back meetings unnecessarily. - Choosing the free room with the earliest end time rather than the lowest index.
- Keeping the original end time for a delayed meeting instead of preserving its duration.
- Sorting by
(start, end)or using an unstable sort, which breaks the input-order tie rule. - Breaking final ties toward the larger index by using
>=in the scan.
Follow-ups worth preparing
- Room capacities. Store free rooms in a segment tree keyed by room index, with each node holding the maximum capacity in its range. Descending left-first finds the lowest-index free room that is large enough in
O(log n). In the waiting case, advance to the next end time, release rooms, and retry. Clarify whether a waiting meeting that doesn't fit blocks later ones. - Cancellations. Use lazy deletion. Tag each heap entry with an assignment id, mark canceled ones stale, and skip stale entries when popping. Operations stay
O(log n)amortized. nfar larger thanm. Used rooms always form a prefix, since roomiis only used when all lower rooms are busy. Keep a counter for the next never-used room plus a heap of rooms that were used and then freed. Setup then costsO(min(n, m))instead ofO(n).