Merging and measuring ranges
Why I'd sort ranges by start time to merge overlaps in one pass, how to avoid the nested-interval bug, and how to insert a new range without sorting again.
Sort by start so every overlap sits next to you
When a question asks you to merge overlapping intervals or measure the total length they cover, I'd usually begin by sorting the bookings on their start time so you can handle everything in a single pass. As you walk through the sorted schedule you really only need to hold onto the current merged block with its opening and closing endpoints. Whenever the next booking starts strictly after your current block finishes you've probably found a real gap (because nothing later in the sorted order can begin any earlier either), so you'd close that block and add its length to your running total before opening a fresh one.
When the next booking begins at or before your current endpoint it overlaps or touches whatever you're
already holding, so you extend your current right boundary using max(end, e) and keep walking.
You'll also want to remember to flush the very last block once the loop finishes, since there isn't any later
gap to trigger it for you. I've seen plenty of capable candidates write a clean sweep loop and accidentally
drop that closing block right on the return line.
Merge and measure in one sweep
coveredLength(bookings)
- I'd sort the bookings by start time and open the first block using the earliest booking.
- For every following booking you check whether its start sits strictly past your current endpoint.
- When there's a gap you close the current block (adding its length to your total) and open a fresh block.
- Otherwise the bookings overlap or touch so you'd update your right boundary to max(end, e) and carry on.
- After the loop finishes you add the final block's length to your running total.
Sorting does almost all of the work here while the sweep itself only visits each booking once, and you'll usually spend linear space on the sorted copy or output list (although you can manage with constant extra memory if you're allowed to sort in place and only return the total).
Here's the trace on five unsorted bookings (6 to 8 and 1 to 3 and 2 to 5 and 10 to 12 and 11 to 13) sorted by start time, merging into 1 to 5 (length 4) and 6 to 8 (length 2) and 10 to 13 (length 3) for a total of 9.
-
Step 1 / 6bookings, sorted by start1–302–516–8210–12311–134block1–3total0
Sort by start time: 1–3, 2–5, 6–8, 10–12, 11–13. The first booking opens a block from 1 to 3.
-
Step 2 / 6bookings, sorted by start1–302–516–8210–12311–134block1–5total0
2–5 starts at or before 3, so it overlaps the block. It extends the block to 5.
-
Step 3 / 6bookings, sorted by start1–302–516–8210–12311–134block6–8total4
6–8 starts after the block ends at 5: a gap. Close 1–5 (length 4); the total is 4. Start a new block.
-
Step 4 / 6bookings, sorted by start1–302–516–8210–12311–134block10–12total6
10–12 starts after the block ends at 8: a gap. Close 6–8 (length 2); the total is 6. Start a new block.
-
Step 5 / 6bookings, sorted by start1–302–516–8210–12311–134block10–13total6
11–13 starts at or before 12, so it overlaps the block. It extends the block to 13.
-
Step 6 / 6bookings, sorted by start1–302–516–8210–12311–134total9
No more bookings: close the last block 10–13 (length 3). Total covered: 9.
The nested-interval bug and boundary rules
The mistake I catch most often when students write a merge loop is overwriting the right boundary
directly with end = e when they ought to write end = max(end, e). Suppose you've got
three bookings running from 1 to 10 and from 2 to 4 and from 6 to 8, so the true merged block runs from 1 to 10
with length 9. If you blindly replace end with 4 when you encounter the nested booking from 2 to
4 then the booking from 6 to 8 suddenly looks like a separate block after a gap, and your loop hands back 3
plus 2 = 5 when the real answer is 9 (losing four units of covered time).
I'd also check how the statement treats touching endpoints before you start coding anything. With standard
half-open time windows like 1 to 4 and 4 to 7 there isn't any gap between them and together they cover 1 to 7
(length 6), which means you'd generally merge whenever the next start sits at or before the current endpoint.
Some questions work with inclusive integer days though, where days 1 to 4 and days 5 to 7 leave no missing day
between them, so in that version you'd probably merge whenever the next start is at most
end + 1 and measure each closed block's width as end - start + 1.
Before I submit any merge loop I always try one short booking buried inside a long one (1 to 10 with 2 to 4 and 6 to 8) alongside two bookings that touch at a single endpoint (1 to 4 and 4 to 7).
Inserting one range into an already-merged list
A common follow-up hands you a list of disjoint blocks already sorted by start time and asks you to
insert a single new range without paying for another sort. I think the cleanest linear-time solution uses
three short loops one after another. First you copy across every block that finishes strictly before your new
range begins, because none of those earlier blocks can possibly touch it. Next you absorb every block whose
start sits at or before your new range's endpoint (widening the new range with min on the left
and max on the right) and append that single combined block. Finally you copy across whichever
remaining blocks begin strictly to the right.
You can test this by inserting 4 to 8 into five separate blocks (1 to 2 and 3 to 5 and 6 to 7 and 8 to 10 and 12 to 16). Because the opening block from 1 to 2 finishes before 4 it passes straight through untouched. The middle three blocks all begin by 8 so they merge together with 4 to 8 into a single combined block running from 3 to 10, while the closing block from 12 to 16 starts after 10 and copies across unchanged, leaving you with three blocks (1 to 2 and 3 to 10 and 12 to 16) in one linear pass.