KatInterview

Maximum Simultaneous Room Bookings

Greedy & IntervalsAuto-graded · 15 test cases
MediumAuto-gradedGreedy & Intervalssweep lineintervalsgreedy

Last updated

Tests the sweep line technique on interval scheduling to find the peak number of overlapping room bookings over a timeline.

Maximum Simultaneous Room Bookings

A conference centre logs every room booking as a half-open interval [start, end) — the room is occupied from minute start up to but not including minute end.

Given a list of such bookings, find the maximum number of bookings that are simultaneously active at any single point in time, and return the earliest minute at which that maximum is first reached.

Formally, for a query minute t, a booking [s, e) is active if s <= t < e. You must return a tuple [peak_count, earliest_minute].

If the booking list is empty, return [0, -1].

Constraints

  • 0 <= len(bookings) <= 2000
  • Each booking is a list [start, end] with 0 <= start < end <= 10^6
  • All start and end values are integers
  • Multiple bookings may share the same start or end
  • There is always a unique answer for earliest_minute because ties are broken by taking the smallest such minute

Examples

Example 1

Input: max_simultaneous_bookings([[1, 5], [2, 6], [4, 8], [7, 10]])

Output: [3, 4]

Example 2

Input: max_simultaneous_bookings([[0, 3], [3, 6], [6, 9]])

Output: [2, 3]

Example 3

Input: max_simultaneous_bookings([])

Output: [0, -1]

Hints

Hint 1

Create two types of events: +1 at each booking's start and -1 at each booking's end, then process them in time order — this is the classic sweep line setup.

Hint 2

When multiple events fall at the same minute, process all +1 (start) events before all -1 (end) events, so a room that starts exactly when another ends counts as overlapping.

Hint 3

Track a running count as you sweep; whenever it exceeds the current maximum, update both the peak count and the earliest minute where it was reached.