Katkat

Simulate meeting-room bookings and return busiest room

GoogleSoftware Engineer · Onsite

Last updated

This problem tests your ability to manage interval-based resource allocation using a priority queue and a frequency map to track usage statistics.

Problem Statement

You are tasked with managing a set of meeting rooms in an office building. You have n rooms, indexed from 0 to n-1. All rooms start empty. Given a list of meetings where each meeting is defined by a [start, end] interval, assign each meeting to an available room according to these rules:

  1. A meeting will take place if there is an available room. If multiple rooms are free, assign the meeting to the room with the lowest index.
  2. If no room is free, the meeting that starts earliest must wait until a room becomes free.
  3. When a room becomes free, it can be used for the next scheduled meeting in the queue.
  4. If multiple meetings are scheduled to start at the same time, process them in the order they appear in the input list.

Your goal is to return the index of the room that held the most meetings. If there is a tie, return the room with the smallest index.

Function Signature

def busiest_room(n: int, meetings: List[List[int]]) -> int:

Examples

Example 1: Input: n = 2, meetings = [[0, 10], [1, 5], [2, 7], [3, 4]]

  • Room 0 takes [0, 10]. Room 1 takes [1, 5].
  • At time 5, Room 1 becomes free. Room 1 takes [2, 7].
  • At time 7, Room 1 becomes free. Room 1 takes [3, 4].
  • Output: 1 (Room 0 held 1, Room 1 held 3)

Constraints

  • 1 <= n <= 50,000
  • 1 <= meetings.length <= 100,000
  • 0 <= start < end <= 10^9

Edge Cases

  • All meetings fit in one room.
  • More meetings than rooms (waitlist logic required).
  • Meetings starting exactly when others end.

Hints

Hint 1

Use a min-heap to keep track of available rooms sorted by their index.

Hint 2

Use a second min-heap to keep track of occupied rooms, storing them as `(end_time, room_index)` tuples.

Hint 3

Before assigning a new meeting, move all rooms that have finished their current meeting from the occupied heap back to the available room heap.

Follow-up questions

  • How would you modify your solution if each room had a different capacity and meetings required a minimum capacity?
  • What if meetings could be canceled dynamically? How would that affect your data structures?
  • How does the time complexity change if `n` is significantly larger than the number of meetings?