Book a Room Without Conflicts

Medium ⏱ 12 min 53% acceptance ★★★★★ 4.6
Write book(calendar, start, end) where calendar is a list of existing (start, end) bookings (end exclusive): append and return True only when the new slot overlaps nothing; otherwise False. Two slots conflict when each starts before the other ends.

Examples

Example 1
Input
book([(9, 10), (11, 12)], 10, 11)
Output
True
Explanation

The 10-11 gap is exactly free (end-exclusive).

Example 2
Input
book([(9, 10), (11, 12)], 9, 12)
Output
False
Explanation

It would swallow both existing meetings.

Constraints

  • Back-to-back bookings are legal.
  • Mutate the calendar only on success.

Topics

Business Logicbooking conflicts

Companies

GoogleMicrosoftZoho

Hints

Hint 1

Conflict test: s < existing_end and existing_start < e.

Hint 2

any() over the calendar.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.