Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Several teams schedule maintenance on the same services, and the SLA report counts downtime by adding up the windows. When two teams book overlapping windows the overlap is counted twice, and last quarter's availability figure was lower than the actual outage.
Write merge_windows(windows). Return the smallest set of windows covering the same time, per service.
Function to write
merge_windows(windows: list[dict]) -> list[dict]One dict per merged window with service, start, end and minutes, ordered by service then start.
How to approach it
Group by service, sort each group by start, then extend or open as you walk it.
Sample cases
+ 2 held back until you submit
a night of maintenance
Unsorted windows that overlap, touch exactly, nest inside one another, and one that ends before it starts.
Input
Argument 1
| service | start | end |
|---|---|---|
| api | 2026-03-02T02:00:00Z | 2026-03-02T03:00:00Z |
| db | 2026-03-02T01:00:00Z | 2026-03-02T01:30:00Z |
| api | 2026-03-02T02:30:00Z | 2026-03-02T04:00:00Z |
| api | 2026-03-02T04:00:00Z | 2026-03-02T05:00:00Z |
| api | 2026-03-02T06:00:00Z | 2026-03-02T06:15:00Z |
| api | 2026-03-02T02:10:00Z | 2026-03-02T02:20:00Z |
| db | 2026-03-02T05:00:00Z | 2026-03-02T04:00:00Z |
Returns
| service | start | end | minutes |
|---|---|---|---|
| api | 2026-03-02T02:00:00+00:00 | 2026-03-02T05:00:00+00:00 | 180 |
| api | 2026-03-02T06:00:00+00:00 | 2026-03-02T06:15:00+00:00 | 15 |
| db | 2026-03-02T01:00:00+00:00 | 2026-03-02T01:30:00+00:00 | 30 |
nothing scheduled
An empty calendar merges to an empty list rather than raising on the first window.
Input
Argument 1
[] (empty list)
Returns
[] (empty list)
two services that never overlap
Two services overlapping in time are still separate windows, because the service is part of the identity.
Input
Argument 1
| service | start | end |
|---|---|---|
| api | 2026-03-02T01:00:00Z | 2026-03-02T02:00:00Z |
| db | 2026-03-02T01:30:00Z | 2026-03-02T02:30:00Z |
Returns
| service | start | end | minutes |
|---|---|---|---|
| api | 2026-03-02T01:00:00+00:00 | 2026-03-02T02:00:00+00:00 | 60 |
| db | 2026-03-02T01:30:00+00:00 | 2026-03-02T02:30:00+00:00 | 60 |
Constraints
Z, with an offset, or with no zone. Convert everything to UTC before comparing, and report in UTC.service, start, end and minutes, the duration rounded to two decimal places.Worked example
The api windows include one ending at 03:00 and another starting at 02:30, which clearly overlap — but also one ending at 04:00 and another starting at exactly 04:00. Those touch without overlapping, and treating them as separate reports two outages where users experienced one continuous two-hour stretch.
The window from 02:10 to 02:20 sits entirely inside the 02:00 to 03:00 one. A merge that always takes the newer end time shortens the outage to 02:20 and reports the service as available while it was down. The rule has to be to extend the end only when the new one is later, which is the difference between a maximum and an assignment.
What this tests
Interval merging with the two subtleties that make it more than a sort: touching counts as overlapping, and a contained interval must not shrink its container. Both produce output that is the right shape and the wrong duration.
merge_windows(windows: list[dict]) -> list[dict]Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.