Merge overlapping intervals after sorting
Overlapping meetings, ranges of free memory and booked dates all reduce to intervals. Sorting by start puts every overlap next to each other, so one pass can merge them.
spans = sorted([(5, 8), (1, 3), (2, 4)])
print(spans) # [(1, 3), (2, 4), (5, 8)]Think before coding
After sorting, an interval either starts inside the last merged one, which extends its end, or starts after it, which closes it and opens a new one. Use max for the end, because a short interval can sit entirely inside a longer one. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write merge(intervals) taking (start, end) tuples. Return the merged, sorted list; touching intervals such as (1, 2) and (2, 3) merge too. Leave the input list unchanged.
The explanation opens here once every check passes.