Quick Overview

A coding question about assigning meetings to numbered rooms: each meeting takes the lowest-numbered free room or waits for the earliest room to free up while keeping its duration, and the answer is the room that hosts the most meetings. It tests event simulation, exact tie-breaking and time values that exceed 32 bits.

Most Used Meeting Room When Meetings Wait for a Free Room

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

A building has `n` meeting rooms numbered `0` to `n - 1`. You are given a list of meetings, each with a start time and an end time, and no two meetings have the same start time. Meetings are assigned to rooms by the rules below; when no room is free, a meeting is delayed rather than dropped. Return the number of the room that hosts the most meetings. The report names this as a variant of a known room-scheduling problem without saying what the variant changed; this statement uses the standard rules. ### Function Signature ```python def most_used_room(n: int, meetings: list[list[int]]) -> int: ``` ### Rules - `meetings[i] = [start, end]` needs one room for the half-open interval `[start, end)`. A room whose meeting ends at time `t` can host a meeting that starts at time `t`. - Meetings are assigned one at a time, in increasing order of their original start times. - If one or more rooms are free at a meeting's start time, the meeting takes the free room with the lowest number. - If no room is free, the meeting is delayed until the earliest time at which a room becomes free, and takes the lowest-numbered room that becomes free at that time. A delayed meeting keeps its original duration `end - start`. - Every meeting is eventually held; none is dropped. - Return the room that hosts the most meetings. If several rooms tie, return the lowest room number. ### Constraints - `1 <= n <= 100` - `1 <= len(meetings) <= 10^5` - `0 <= start < end <= 5 * 10^5` for every meeting - All `start` values are distinct; `meetings` may be given in any order. - Delays accumulate, so a delayed meeting can end as late as about `5 * 10^10`, which exceeds `2^31 - 1`. Use 64-bit integers for times; every value stays well below `2^53`. ### Examples **Example 1** ```text Input: n = 2, meetings = [[0, 10], [1, 3], [2, 4], [5, 6]] Output: 1 ``` Room 0 takes `[0, 10)` and room 1 takes `[1, 3)`. At time 2 both rooms are busy, so `[2, 4)` waits for room 1, which frees at 3, and runs as `[3, 5)`. At time 5 room 1 is free again and takes `[5, 6)`. Room 1 hosts three meetings and room 0 hosts one. **Example 2** ```text Input: n = 3, meetings = [[1, 4], [2, 3], [3, 7], [4, 5]] Output: 0 ``` `[1, 4)` takes room 0 and `[2, 3)` takes room 1. At time 3 rooms 1 and 2 are free, so `[3, 7)` takes room 1. At time 4 rooms 0 and 2 are free, so `[4, 5)` takes room 0. Rooms 0 and 1 each host two meetings, and the tie goes to room 0. **Example 3** ```text Input: n = 2, meetings = [[0, 5], [3, 6], [2, 4], [1, 5], [8, 9]] Output: 0 ``` In start order, `[0, 5)` takes room 0 and `[1, 5)` takes room 1. `[2, 4)` must wait; both rooms become free at 5, so it takes room 0 and runs as `[5, 7)`. `[3, 6)` waits for room 1, free at 5, and runs as `[5, 8)`. At time 8 both rooms are free, so `[8, 9)` takes room 0. Room 0 hosts three meetings and room 1 hosts two.

Overview: A coding question about assigning meetings to numbered rooms: each meeting takes the lowest-numbered free room or waits for the earliest room to free up while keeping its duration, and the answer is the room that hosts the most meetings. It tests event simulation, exact tie-breaking and time values that exceed 32 bits.

A building has `n` meeting rooms numbered `0` to `n - 1`. You are given a list `meetings` where `meetings[i] = [start, end]` describes a meeting that needs one room for the half-open interval `[start, end)`. No two meetings share a start time, and `meetings` may be given in any order. Meetings are assigned to rooms by these rules: - Meetings are assigned one at a time, in increasing order of their original start times. - A room whose meeting ends at time `t` can host a meeting that starts at time `t`. - If one or more rooms are free at a meeting's start time, the meeting takes the free room with the lowest number. - If no room is free, the meeting is delayed until the earliest time at which a room becomes free, and it takes the lowest-numbered room that becomes free at that time. A delayed meeting keeps its original duration `end - start`. - Every meeting is eventually held; none is dropped. Return the number of the room that hosts the most meetings. If several rooms tie, return the lowest room number. Implement `most_used_room(n, meetings)`, which returns that room number as an integer. ### Constraints - `1 <= n <= 100` - `1 <= len(meetings) <= 10^5` - `0 <= start < end <= 5 * 10^5` for every meeting - All `start` values are distinct; `meetings` may be given in any order. - Delays accumulate, so a delayed meeting can end as late as about `5 * 10^10`, which exceeds `2^31 - 1`. Use 64-bit integers for times (`long` in Java, `long long` in C++); every value stays well below `2^53`. The input times and the returned room number fit in 32-bit integers. ### Example 1 ```text Input: n = 2, meetings = [[0, 10], [1, 3], [2, 4], [5, 6]] Output: 1 ``` Room 0 takes `[0, 10)` and room 1 takes `[1, 3)`. At time 2 both rooms are busy, so `[2, 4)` waits for room 1, which frees at 3, and runs as `[3, 5)`. At time 5 room 1 is free again and takes `[5, 6)`. Room 1 hosts three meetings and room 0 hosts one. ### Example 2 ```text Input: n = 2, meetings = [[0, 5], [3, 6], [2, 4], [1, 5], [8, 9]] Output: 0 ``` In start order, `[0, 5)` takes room 0 and `[1, 5)` takes room 1. `[2, 4)` must wait; both rooms become free at 5, so it takes room 0 and runs as `[5, 7)`. `[3, 6)` waits for room 1, free at 5, and runs as `[5, 8)`. At time 8 both rooms are free, so `[8, 9)` takes room 0. Room 0 hosts three meetings and room 1 hosts two.

Constraints

  • 1 <= n <= 100
  • 1 <= len(meetings) <= 10^5
  • 0 <= start < end <= 5 * 10^5 for every meeting [start, end]
  • All start values are distinct; meetings may be given in any order.
  • A delayed meeting can end as late as about 5 * 10^10, which exceeds 2^31 - 1: use 64-bit integers for times (long in Java, long long in C++); every value stays well below 2^53.

Examples

Input: (1, [[0, 1]])

Expected Output: 0

Explanation: Minimum valid input: one room and one meeting, so room 0 hosts it.

Input: (5, [[3, 8]])

Expected Output: 0

Explanation: A single meeting with five free rooms takes the lowest-numbered free room, 0.

Hints

  1. Meetings are handled in increasing order of their original start times, not in the order they are given.
  2. For each room it is enough to know the time it next becomes free; a room that frees exactly at a meeting's start time counts as free.
  3. A meeting that has to wait starts when its room frees and keeps its original duration, so its new end can grow far beyond the input times.

Loading coding console...

Show the approach

Approach

Simulate the assignment in the order the rules fix. Sort the meetings by start time (starts are distinct, so this order is unique). Keep two min-heaps: the numbers of the free rooms, and the busy rooms keyed by (time the room becomes free, room number). For each meeting [start, end], first move every busy room whose free time is <= start into the free heap; intervals are half-open, so a room that frees exactly at start is free. If the free heap is non-empty, the meeting takes its smallest room number and that room becomes busy until end. Otherwise every room is busy past start, and the top of the busy heap is the room with the smallest free time and, among rooms freeing at that same instant, the lowest number, which is exactly the room the delay rule picks; the meeting starts at that free time and keeps its duration, so the room becomes free at free_time + (end - start). Count one meeting for the chosen room each time, then return the lowest-numbered room with the maximum count (a strict > scan from room 0 keeps the lowest on ties).

Invariant: before each meeting, every room is in exactly one heap, the free heap holds only rooms whose free time is <= the current start (starts only increase, so a room once free stays free), and the busy heap holds each other room with its exact free time. Each step therefore makes the choice the rules prescribe, so by induction the counts match the rules.

Edge cases: n = 1 (every overlapping meeting waits in room 0), more rooms than meetings (nothing waits), meetings given out of order (sorting first), ties in meeting count (lowest room), and accumulated delays, which can push free times to about 5 * 10^10, past 2^31 - 1, so times are 64-bit (long / long long; JavaScript numbers are exact below 2^53). The JavaScript reference replaces the heaps with a scan over the at most 100 rooms: the lowest room whose free time is <= start, otherwise the room with the smallest free time (lowest index on ties), which makes the same choices in O(m * n).

Time complexity:
O(m log m + m log n), where m = len(meetings)
Space complexity:
O(m + n)