Busiest Conference Room Tracker
Heaps, Sorting & SearchAuto-graded · 22 test casesLast updated
Tests the ability to simulate event scheduling with delayed assignments using two priority queues to track which resource handles the most requests.
Problem Statement
A building has num_rooms conference rooms, numbered 0 through num_rooms - 1. You are given a list of bookings, where bookings[i] = [start_i, end_i] describes a session that was originally requested to run from time start_i up to (but not including) time end_i.
The building's scheduling system assigns sessions to rooms using the following policy:
- Sessions are processed in ascending order of their requested start times. No two sessions share the same requested start time.
- A session is placed in the lowest-numbered room that is free at or before the session's requested start time.
- If every room is still occupied when a session's turn comes up, that session is postponed until the earliest moment a room opens. The session keeps its original length (
end_i - start_i) even though it starts late. - When two or more rooms open at the same moment, the waiting session goes to the one with the lowest number.
- If multiple sessions are waiting, they are assigned in the order of their original requested start times.
Return the number of the room that hosted the greatest number of sessions overall. If two or more rooms are tied for the most sessions, return the one with the smallest number.
Constraints
1 <= num_rooms <= 1001 <= len(bookings) <= 10^5bookings[i].length == 20 <= start_i < end_i <= 10^9- All
start_ivalues are distinct.
Examples
Example 1
Input: busiest_conference_room(2, [[0, 10], [1, 5], [2, 7], [3, 4]])
Output: 0
Example 2
Input: busiest_conference_room(3, [[1, 20], [2, 10], [3, 5], [4, 9], [6, 8]])
Output: 1
Hints
Hint 1
Sort the bookings by start time first. Then think about what ordering you need when picking a free room versus when picking among occupied rooms — are those the same ordering?
Hint 2
A min-heap of free room indices gives you the lowest-numbered available room in O(log n). A separate min-heap of (finish_time, room_index) tuples gives you the room that opens soonest, with ties broken by index automatically.
Hint 3
When a session must be delayed, the new finish time is earliest_release + (end - start), not just `end`. Also remember to release rooms whose finish time is <= the current session's start time before deciding whether any room is free.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that two different orderings are needed at the same time:
- Among free rooms, we always want the one with the lowest index.
- Among occupied rooms, we want the one that finishes soonest (and, on a tie, the lowest index).
These two needs map directly onto two separate min-heaps.
Data structures
free: a min-heap of room indices. Popping always yields the smallest available room number.occupied: a min-heap of(finish_time, room_index)tuples. Python compares tuples lexicographically, so ties in finish time are broken by room index automatically — which is exactly what rule 4 requires.
Simulation loop
Sort bookings by start time (all start times are distinct, so the order is unambiguous).
For each session [start, end]:
-
Release rooms. Pop every entry from
occupiedwhosefinish_time <= startand push that room index back intofree. The<=is intentional: because end times are exclusive, a room finishing at timetis available for a session that begins att. -
Assign a room.
- If
freeis non-empty, pop the minimum (lowest index). The session ends at its originalend. - Otherwise, pop the minimum of
occupiedto get(earliest_finish, room). The session is delayed and runs for its original duration, so the new finish time isearliest_finish + (end - start).
- If
-
Increment that room's counter and push
(finish, room)ontooccupied.
Why the algorithm is correct
Rule 2 — After the release step, free contains exactly the rooms available at or before start. The heap minimum is the lowest-numbered one.
Rules 3 & 4 — When free is empty, every room is occupied past start. The session must wait for occupied[0], which is the earliest-finishing room with the lowest index on ties.
Rule 5 — Because meetings are processed in sorted start-time order, a later session can never receive an earlier effective start than an earlier session. If session A is delayed to time T, any session B with a later original start also finds all rooms busy at least until T, so B is delayed to T or beyond. The sorted processing order alone enforces FIFO among delayed sessions; no separate queue is needed.
Tie-breaking on the final answer — max(range(num_rooms), key=lambda r: (session_count[r], -r)) picks the largest count, and among equal counts, the largest -r corresponds to the smallest room number.
Complexity
Let m = len(bookings).
- Time: O(m log m + m log n) — O(m log m) to sort, then each session performs O(log n) heap operations. Each room enters and leaves
occupiedat most once per session it hosts, so the release loop is O(1) amortized per session. - Space: O(m + n) — O(m) for the sorted copy, O(n) for the two heaps and the counts array.
Edge cases
num_rooms = 1: every session chains back-to-back in room 0; the answer is always 0.- A session starts exactly when another ends: the
<=in the release step correctly frees the room in time. - Delayed finish times can exceed
10^9(up to roughly10^14). Python's arbitrary-precision integers handle this; in statically typed languages, use 64-bit integers. - Rooms that receive no sessions keep a count of 0 and cannot win unless all rooms have 0 (impossible if there is at least one session).
Reference solution
import heapq
from typing import List
def busiest_conference_room(num_rooms: int, bookings: List[List[int]]) -> int:
# Min-heap of free room indices — always yields the lowest-numbered free room.
free = list(range(num_rooms))
heapq.heapify(free)
# Min-heap of (finish_time, room_index) for currently occupied rooms.
# Tuple ordering breaks ties by room index, satisfying rule 4.
occupied = []
session_count = [0] * num_rooms
for start, end in sorted(bookings):
# Release every room whose session has finished by this start time.
# end times are exclusive, so finish <= start means the room is free.
while occupied and occupied[0][0] <= start:
_, room = heapq.heappop(occupied)
heapq.heappush(free, room)
if free:
# At least one room is available — take the lowest-numbered one.
room = heapq.heappop(free)
finish = end
else:
# No room is free; delay the session until the earliest release.
finish, room = heapq.heappop(occupied)
finish += end - start # preserve original duration
session_count[room] += 1
heapq.heappush(occupied, (finish, room))
# Return the room with the highest count; smallest index wins ties.
return max(range(num_rooms), key=lambda r: (session_count[r], -r))