Find Minimum Rooms Needed
GoogleSoftware Engineer · Technical ScreenLast updated
Tests interval processing, sorting, and tracking concurrent overlapping ranges using heaps or two-pointer sweep-line techniques.
Problem Statement
You are given an array of meeting time intervals intervals where each interval is represented as a pair of integers [start, end]. Each pair signifies that a meeting begins at start and finishes at end (start < end).
A single room can host at most one meeting at any given time. If a meeting ends at time T, another meeting can begin in the same room at time T without conflict.
Write a function that calculates the minimum number of conference rooms required to schedule all meetings without overlap.
Interface
def min_meeting_rooms(intervals: list[list[int]]) -> int:
pass
Examples
Example 1
Input:
intervals = [[0, 30], [5, 10], [15, 20]]
Output:
2
Explanation:
- Room 1 hosts
[0, 30]. - Meeting
[5, 10]conflicts with[0, 30], so it needs Room 2. - Meeting
[15, 20]starts after[5, 10]ends at time10, so it can reuse Room 2. Total rooms needed:2.
Example 2
Input:
intervals = [[7, 10], [2, 4]]
Output:
1
Explanation: The meetings do not overlap in time, so a single room can host both.
Constraints
1 <= len(intervals) <= 10^5intervals[i].length == 20 <= intervals[i][0] < intervals[i][1] <= 10^9
Edge Cases to Consider
- Consecutive meetings that share a boundary (e.g.,
[1, 5]and[5, 10]) do not require separate rooms. - Completely nested intervals (e.g.,
[1, 10]and[2, 5]). - Multiple intervals starting or ending at identical times.
Hints
Hint 1
Consider the problem as tracking the number of active, simultaneous meetings over time.
Hint 2
Try sorting the meetings by their start times and using a min-heap to keep track of the earliest ending time of currently occupied rooms.
Hint 3
Alternatively, separate the start and end times into two sorted arrays and use a two-pointer sweep-line approach to count overlaps.
Follow-up questions
- How would you modify your algorithm to return an actual room assignment schedule (e.g., assigning specific room IDs 1, 2, ... to each meeting)?
- How would you handle this problem if intervals arrive in a real-time stream rather than as a fixed batch?
- If all timestamps are guaranteed to fall within a single 24-hour day with minute-level precision (1440 minutes), can you achieve O(N) runtime?
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.