Katkat

Simulate room assignments for scheduled meetings

GoogleSoftware Engineer · Technical Screen

Last updated

This problem tests your ability to manage resources over time using a priority queue and sorting, focusing on handling events and tie-breaking logic for room availability.

Problem Statement

You are given a set of meetings, where each meeting is represented as [start_time, end_time]. You have n meeting rooms labeled 0 to n-1. All rooms are initially empty.

When a meeting is scheduled, follow these rules:

  1. If a room becomes free, the meeting is assigned to the room with the smallest index.
  2. If no room is free at the meeting's start_time, the meeting is delayed until the first available room becomes free. If multiple rooms become free at the same time, the meeting is assigned to the room with the smallest index.
  3. If multiple meetings start at the same time, process them in the order of their original start_time (or based on the input order if start times are identical).

Your task 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 mostBooked(n: int, meetings: list[list[int]]) -> int:

Constraints

  • 1 <= n <= 100
  • 1 <= meetings.length <= 10^5
  • 1 <= start_time < end_time <= 10^5

Example 1

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

  • Room 0 takes [0,10].
  • Room 1 takes [1,5].
  • Meeting [2,7] is delayed until time 5 (Room 1).
  • Meeting [3,4] is delayed until time 10 (Room 0).
  • Room 0 held 2 meetings, Room 1 held 2 meetings. Return 0.

Edge Cases

  • n is greater than the number of meetings.
  • All meetings have the same start time.

Hints

Hint 1

Sort the meetings by start time to process them chronologically.

Hint 2

Use two priority queues: one for idle rooms (storing room indices) and one for busy rooms (storing end time and room index).

Hint 3

If no rooms are idle, extract the room that finishes earliest from the busy queue and calculate the new end time for the incoming meeting.

Follow-up questions

  • How would you optimize the solution if n was up to 10^5 instead of 100?
  • If each room had a different 'cost' to book, how would your selection strategy change?
  • How would the logic change if meetings could be preempted or canceled?