Sort by start and extend
Coalesce Closed Coverage Spans
01. The question
Given unsorted closed [start,end] coverage spans, return their sorted coalesced union. Touching endpoints merge, nested spans do not create new output rows, and each input start is strictly less than its end.
Before revealing the idea, predict the result and describe what information you would need to track.
02. Worked example
Input[[7,9],[1,4],[4,8],[12,15]]
Output[[1,9],[12,15]]
- Sort into [1,4], [4,8], [7,9], [12,15].
- The first three overlap or touch, so their union extends to 9.
- A gap before 12 starts a second output span.
Try your code
Define solve(data, parameter="") and return your result. Your code runs locally, not on a server.
Edit your test cases
Use 1-10 objects with data and an optional string parameter. Expected outputs are computed from the lesson. An optional expected field must be correct.
Input contract & reference code
Example data: [[7,9],[1,4],[4,8],[12,15]]. Parameter: "". Numeric parameters arrive as strings; convert them with int(parameter) when needed.
The example and generated cases use the question’s exact variant. This checks behavior on a small test batch—not performance on every possible input.
Python downloads only when you run code, using compressed runtime assets. Code is not saved or uploaded. Enable JavaScript to run it.
Printed output
Five-second execution limit per batch. Standard-library Python only; additional packages and network access are unavailable. Stop ends execution without blocking the lesson.
03. The idea
Try sketching your own approach first. What would you track, and why would it work?
Reveal the idea
- Sort by start, then end.
- Append a span when it is strictly beyond the last output end.
- Otherwise extend only the last output end.
04. Watch it happen
Use Step to move slowly, or Play to follow the whole trace. Change a preset or load your own input.
Input & settings
Red = current item / blue = active window or cells / green = completed work. A value docks at its actual destination; the original input stays unchanged. Positions start at 0.
Working state / what changes
Ready to begin
Enable JavaScript to step through the animation. The worked example and solution below remain readable without it.
Python / why it changes
The line behind the step
def solve(data, parameter=''):
result = []
for start, end in sorted(data):
if not result or start > result[-1][1]:
result.append([start, end])
else:
result[-1][1] = max(result[-1][1], end)
return resultHow to use this walkthrough
Hover or focus a control for its label. Play forms the input and follows values into the working state. Step shows one change; Back and the slider restore exact snapshots. Reduced motion uses instant updates. Saved snapshots and sorted heap displays are teaching overhead, not algorithm complexity.
05. Python solution
def solve(data, parameter=''):
result = []
for start, end in sorted(data):
if not result or start > result[-1][1]:
result.append([start, end])
else:
result[-1][1] = max(result[-1][1], end)
return result06. Cost & edge cases
- Time
- O(n log n)
- Space
- O(n), including sorting and returned spans
- Empty input returns [].
- Duplicate or nested spans are absorbed.
- Closed [1,3] and [3,5] merge into [1,5].