The question asks for the maximum number of simultaneous intervals, and meetings that end before the next start release reusable capacity.
Keep true
Immediately before pushing a meeting, the heap contains exactly the end times greater than its start; after pushing, heap size equals active rooms and most_rooms is the largest size seen.
Reuse it
For capacity over time, sweep arrivals and keep active expirations ordered by the next one to finish; this transfers to server concurrency, platform usage, and resource reservation.
Sort meetings by start. Begin with an empty heap and zero peak. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why are three rooms required even though only two end times remain in the final heap?
Sources and provenance
Full interview answer 18 min read
Find the fewest rooms needed to hold every meeting without putting overlapping meetings in the same room.
Problem
Input. A list intervals of integer meeting pairs [start, end], each satisfying start < end.
Output. Return the smallest number of rooms that accommodates every meeting at its specified time.
Rules. Each meeting occupies a half-open interval [start, end) and needs one room throughout.
A room may be reused exactly when its previous meeting ends.
Meetings cannot move in time. Empty input returns zero; zero-duration meetings are outside this contract.
Example.[[0, 30], [5, 10], [5, 15], [20, 25]] returns 3 because three meetings overlap at time 5.
For [[0, 5], [5, 10]], return 1.
Picture
Process starts in sorted order.
The heap stores end times for meetings still active at the current start.
Its root is the earliest end, not the end of the most recently added meeting.
Next meeting
Released ends
Active end times after insertion
Largest count
[0,30]
None
30
1
[5,10]
None
10,30
2
[5,15]
None
10,15,30
3
[20,25]
10, then 15
25,30
3
The table lists active values in order for inspection; heap storage need not be fully sorted.
Before time 20, release both expired meetings while retaining the meeting ending at 30.
Current occupancy becomes two, but the earlier peak remains three.
Any simultaneous meetings require distinct rooms, giving a lower bound on capacity.
Reusing a released room, or allocating one when none is free, constructs a schedule meeting that bound.
Problem trace
Separate occupancy checkpoints: peak demand survives a smaller final heap
Read it this way:Near the middle checkpoint, two meeting intervals overlap. Later only one remains active, but the earlier overlap still requires two rooms. Removing finished meetings changes the current occupancy, not the saved peak. Can the smaller occupancy at the last checkpoint retroactively eliminate the earlier need for two rooms?
Reference drawing
Problem trace
Peak overlap determines room demand, not the final heap size
Execution trace
Input and goalFind the smallest number of rooms needed for all meetings.
Initialize the active heapSort meetings by start. Begin with an empty heap and zero peak.
The question asks for the maximum number of simultaneous intervals, and meetings that end before the next start release reusable capacity.
Keep true
Immediately before pushing a meeting, the heap contains exactly the end times greater than its start; after pushing, heap size equals active rooms and most_rooms is the largest size seen.
Reuse it
For capacity over time, sweep arrivals and keep active expirations ordered by the next one to finish; this transfers to server concurrency, platform usage, and resource reservation.
Sort meetings by start. Begin with an empty heap and zero peak. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why are three rooms required even though only two end times remain in the final heap?
Read it this way:Near the middle checkpoint, two meeting intervals overlap. Later only one remains active, but the earlier overlap still requires two rooms. Removing finished meetings changes the current occupancy, not the saved peak. Can the smaller occupancy at the last checkpoint retroactively eliminate the earlier need for two rooms?
Code
import heapqdef min_meeting_rooms(intervals: list[list[int]]) -> int: end_times: list[int] = [] most_rooms = 0 for start, end in sorted(intervals): while end_times and end_times[0] <= start: heapq.heappop(end_times) heapq.heappush(end_times, end) most_rooms = max(most_rooms, len(end_times)) return most_rooms
Each end enters and leaves the heap at most once.
Sorting and heap updates take O(nlogn) scalar work and O(n) storage, including the sorted input copy.
The original list stays unchanged; empty input returns 0.
Failure Check
Returning the final heap size gives 2, missing the earlier demand for three rooms.
Replacing while with if leaves expired 15 before time 20, breaking this exact-active-population invariant.
A room-allocation algorithm may use another invariant; do not describe its retained heap as the current active population.
Change Endpoint Occupancy
New task: Both endpoints occupy the room at their exact timestamp.
Picture and change: Release an end only when it is strictly earlier than the new start.
The peak-count argument stays unchanged.
import heapqdef closed_meeting_rooms(intervals): active, peak = [], 0 for start, end in sorted(intervals): while active and active[0] < start: heapq.heappop(active) heapq.heappush(active, end) peak = max(peak, len(active)) return peak
Check:[[0,1],[1,2]] needs two rooms because both meetings occupy time 1.
The half-open reference needs one.
Positive duration remains required; this change does not introduce a zero-duration policy.
Use when: The occupancy contract makes touching endpoints conflict.
Change to a Room Assignment
New task: Return room IDs in original input order, together with the minimum allocated count.
Picture and change: Store (end,room) in the active heap and released IDs in a second min-heap.
Keep original indices while sorting, and allocate a new ID only when no released ID exists.
import heapqdef assign_meeting_rooms(intervals): active, free = [], [] assigned = [None] * len(intervals) room_count = 0 for index, (start, end) in sorted(enumerate(intervals), key=lambda item: item[1]): while active and active[0][0] <= start: ended, room = heapq.heappop(active) heapq.heappush(free, room) if free: room = heapq.heappop(free) else: room = room_count room_count += 1 assigned[index] = room heapq.heappush(active, (end, room)) return room_count, assigned
Check: The source example returns (3,[0,1,2,1]).
The last meeting reuses room 1; meetings sharing any room must satisfy previous_end <= next_start.
Both variants retain O(nlogn) work and O(n) storage under scalar endpoint costs.
Use when: The capacity answer needs a concrete assignment witness.
Transfer Check
Why can one last_end solve compatible interval selection but not represent this active population?
Answer
Several concurrent meetings expire at different times.
Room demand needs their population and next expiration, not one chosen interval’s compatibility frontier.