Minimum number of rooms needed to hold overlapping meetings
OracleSoftware Engineer · OnsiteLast updated
A coding question that asks for the minimum number of rooms needed so that every meeting in an unsorted list of half-open time intervals can be held without overlap. It tests reasoning about overlapping intervals, the boundary rule for meetings that end and start at the same time, and an efficient solution for large inputs.
You are given a list of meetings, each with a start time and an end time. A room can host at most one meeting at a time, and a meeting stays in the same room from its start to its end. Return the minimum number of rooms needed so that every meeting can be held in full.
Function Signature
def min_meeting_rooms(intervals: list[list[int]]) -> int:
intervals[i] = [start, end] describes meeting i, which occupies the half-open time interval [start, end).
Rules
- Two meetings conflict when their intervals overlap. A meeting that ends at time
tdoes not conflict with a meeting that starts at timet, so the two can use the same room. - The meetings are given in no particular order.
- Return
0when the list is empty.
Constraints
0 <= len(intervals) <= 10^50 <= start < end <= 10^9for every meeting
Examples
Example 1
Input: intervals = [[1, 5], [2, 6], [4, 8], [7, 9]]
Output: 3
Between times 4 and 5, the meetings [1, 5), [2, 6) and [4, 8) are all in progress, so three rooms are needed. Three is enough: by time 7 the meeting [2, 6) has ended, so [7, 9) can use its room.
Example 2
Input: intervals = [[1, 3], [3, 5], [5, 7]]
Output: 1
Each meeting starts exactly when the previous one ends, so one room hosts all three.
Example 3
Input: intervals = [[10, 20], [0, 10], [5, 15]]
Output: 2
[5, 15) overlaps both other meetings, but [0, 10) and [10, 20) do not overlap each other and can share a room.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.