KatInterview

Locate the Lowest-Indexed Room That Can Fit a Meeting

Greedy & IntervalsAuto-graded · 19 test cases
MediumAuto-gradedGreedy & Intervalsintervalsbinary-searchsorting

Last updated

Tests interval overlap checks under half-open semantics and using binary search on sorted, disjoint bookings to find a free room quickly.

You manage several rooms. Each room has a schedule of existing bookings. Within a room, the bookings are sorted by start time and never overlap. Every booking is a half-open interval [start, end), so a booking covers times start up to but not including end.

You are also given a new meeting request meeting = [s, e], also half-open. Find the lowest room index where this meeting can be added without overlapping any existing booking in that room. If no room can accommodate the meeting, return -1.

Two half-open intervals overlap only when they share at least one moment in time. A meeting that starts exactly when another one ends (or ends exactly when another one starts) does not overlap it.

Constraints:

  • 0 <= len(rooms) <= 10^4
  • Each room's bookings are sorted by start time and are pairwise non-overlapping.
  • The total number of bookings across all rooms is at most 10^5.
  • meeting has length 2 with s < e.
  • All times are integers in the range 0 to 10^9.
  • Aim for a solution faster than checking every booking in every room.

Examples

Example 1

Input: find_open_room([[[1, 3], [5, 7]], [[2, 4], [8, 10]]], [3, 5])

Output: 0

Hints

Hint 1

Two half-open intervals [a, b) and [s, e) collide exactly when a < e and s < b. Make sure your check treats touching endpoints as non-colliding.

Hint 2

In a sorted, disjoint list, use binary search to find the first booking whose start is at least s. Only the booking just before it and the one at that position can possibly collide with the meeting.

Hint 3

Iterate rooms in index order and return the first index that passes the check. Avoid building a separate list of start times for each room, since that costs linear time per room.