Merge overlapping instrument bookings

Target time: 25 minutes.

Problem

The plate reader's booking system stores each booking as (start, end) in minutes from midnight. The end is exclusive, so (60, 90) covers minutes 60 to 89. Bookings arrive in any order and can overlap.

Write busy_blocks(bookings), which returns the times the reader is busy as a list of non-overlapping (start, end) tuples, sorted by start. Bookings that overlap or touch become one block, so (60, 90) and (90, 120) become (60, 120).

>>> busy_blocks([(90, 120), (60, 100), (200, 230)])
[(60, 120), (200, 230)]

How to work it like an interview

  1. Before coding, say the edge cases out loud: no bookings, one booking, one booking inside another, bookings that only touch.
  2. Work one small example by hand.
  3. Then code it. Aim for better than O(n²): there is a speed test.

Files

  • bookings.py
  • test_bookings.py (the tests are correct; don't change them)