codejump
HardSQL

Merge Nested Service Outages

SQLDatabasesGaps and Islands

Outages are intervals [start_minute, end_minute) with start_minute < end_minute. Per service, merge all overlapping or touching intervals into maximal outages. Intervals may be nested or duplicated, and input is unordered. Return service_id, start_minute, end_minute for each merged outage, ordered by service_id, start_minute. A nested interval must not make you lose the end of a longer earlier interval.

Schema (all columns are NOT NULL unless explicitly nullable):

CREATE TABLE outages (outage_id INTEGER PRIMARY KEY, service_id INTEGER NOT NULL, start_minute INTEGER NOT NULL, end_minute INTEGER NOT NULL);

Write one read-only SQLite query. The tables are already populated. Return exactly the requested columns and ordering, with no headers. Empty input produces no rows unless specified otherwise.

Example setup:

INSERT INTO outages VALUES (1,1,0,10),(2,1,10,20),(3,1,25,30);

Expected output:

1|0|20
1|25|30

Timebox: 9 minutes. Focus on correctness at boundaries and duplicate handling.

Sample tests

Test #1Touching intervals merge
Input: "CREATE TABLE outages (outage_id INTEGER PRIMARY KEY, service_id INTEGER NOT NULL, start_minute INTEGER NOT NULL, end_minute INTEGER NOT NULL);\nINSERT INTO outages VALUES (1,1,0,10),(2,1,10,20),(3,1,25,30);"
Output: "1|0|20\n1|25|30"
Test #2Nested interval trap
Input: "CREATE TABLE outages (outage_id INTEGER PRIMARY KEY, service_id INTEGER NOT NULL, start_minute INTEGER NOT NULL, end_minute INTEGER NOT NULL);\nINSERT INTO outages VALUES (1,1,0,100),(2,1,10,20),(3,1,30,40);"
Output: "1|0|100"