Find the Most Frequently Used Meeting Room
AmazonSoftware Engineer · Technical ScreenLast updated
Simulate meeting room assignments with delayed starts and duration preservation using priority queues to find the most utilized room.
Problem Statement
You are given an integer n representing the number of meeting rooms numbered from 0 to n - 1, and a list of intervals meetings where meetings[i] = [start_i, end_i] represents a meeting scheduled to run from time start_i to end_i (exclusive).
Meetings are scheduled according to the following rules:
- Meetings are processed in increasing order of their original start times. All original start times are unique.
- Each meeting is assigned to the unused room with the lowest room number that is free at or before the meeting's scheduled start time.
- If no room is free when a meeting is scheduled to start, the meeting is delayed until at least one room becomes free. The meeting retains its original duration (
end_i - start_i). - When multiple rooms become free at the same time, the meeting is assigned to the room with the lowest room number.
- If multiple meetings are delayed, they are scheduled in the order of their original start times.
Return the room number of the room that hosted the most meetings. If there is a tie, return the smallest room number.
Function Signature
def most_frequently_used_room(n: int, meetings: list[list[int]]) -> int:
pass
Input and Output Format
- Input:
n: An integer representing the number of available rooms.meetings: A list of lists of two integers[start, end]representing the original intervals.
- Output:
- An integer representing the index of the most used room.
Examples
Example 1
Input: n = 2, meetings = [[0, 10], [1, 5], [2, 7], [3, 4]]
Output: 0
Explanation:
- At time 0, meeting [0, 10] starts in room 0.
- At time 1, meeting [1, 5] starts in room 1.
- At time 2, meeting [2, 7] is delayed since both rooms are occupied.
- At time 3, meeting [3, 4] is delayed.
- At time 5, room 1 becomes free. Meeting [2, 7] starts in room 1 for duration 5 (ends at time 10).
- At time 10, both room 0 and room 1 become free. Meeting [3, 4] takes room 0 (lowest index) for duration 1 (ends at time 11).
Room 0 held 2 meetings; room 1 held 2 meetings. Tie goes to room 0.
Example 2
Input: n = 3, meetings = [[1, 20], [2, 10], [3, 5], [4, 9], [6, 8]]
Output: 1
Explanation:
- At time 1, meeting [1, 20] starts in room 0.
- At time 2, meeting [2, 10] starts in room 1.
- At time 3, meeting [3, 5] starts in room 2.
- At time 4, meeting [4, 9] is delayed.
- At time 5, room 2 becomes free. Meeting [4, 9] starts in room 2 (duration 5, ends at 10).
- At time 6, meeting [6, 8] is delayed.
- At time 10, rooms 1 and 2 become free. Meeting [6, 8] starts in room 1 (lowest index, ends at 12).
Room 0 held 1 meeting, room 1 held 2 meetings, room 2 held 2 meetings. Tie between room 1 and 2 goes to room 1.
Constraints
1 <= n <= 1001 <= len(meetings) <= 10^5meetings[i].length == 20 <= start_i < end_i <= 10^9- All
start_iare distinct.
Hints
Hint 1
Sort the meetings by their original start time so that you can simulate assignments sequentially.
Hint 2
Use two min-heaps: one for tracking available room numbers (sorted by room index) and one for occupied rooms (sorted by end time, then room index).
Hint 3
When a meeting cannot start on time, fast-forward time to the earliest room release time, free up all rooms that finish by that time, and extend the current meeting's end time by the delay.
Follow-up questions
- How would you handle meetings having specific capacity requirements and rooms having varying capacities?
- What if meetings could be canceled if their wait time exceeds a predefined threshold?
- How would you scale this simulation to handle millions of streaming meeting requests across distributed servers?
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.