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
The maintenance calendar for a database is kept as a sorted list of non-overlapping [start, end] windows, in minutes. A team adds one more window. The calendar has to stay sorted and non-overlapping, so any windows that the new one overlaps or touches must merge with it.
Write insert_window(windows, new). It takes windows, an already sorted list of non-overlapping [start, end] lists, and new, one [start, end] list. It returns a new list with the new window added and merged. This is not merge-maintenance-windows: here the list is already tidy, and the whole point is to use that to finish in one pass.
Function to write
insert_window(windows: list[list[int]], new: list[int]) -> list[list[int]]A new sorted list of non-overlapping [start, end] windows that includes the new window, with touching windows merged.
How to approach it
Split the list into three groups: windows entirely before the new one, windows that overlap or touch it, and windows entirely after. Grow the new window over the middle group.
Sample cases
+ 3 held back until you submit
a window that bridges two
The new window overlaps the first two entries, so [1, 3] and [6, 9] collapse into [1, 9].
Input
Argument 1
[
[
1,
3
],
[
6,
9
],
[
12,
15
]
]Argument 2
[
2,
7
]Returns
[
[
1,
9
],
[
12,
15
]
]a window that fits in a gap
It touches nothing, so it is inserted in order and the others are untouched.
Input
Argument 1
[
[
1,
3
],
[
10,
12
]
]Argument 2
[
5,
7
]Returns
[
[
1,
3
],
[
5,
7
],
[
10,
12
]
]a window that only touches
Closed ranges: 5 and 10 are shared endpoints, so all three entries merge into [1, 12].
Input
Argument 1
[
[
1,
5
],
[
10,
12
]
]Argument 2
[
5,
10
]Returns
[
[
1,
12
]
]an empty schedule
With nothing scheduled, the answer is just the new window.
Input
Argument 1
[] (empty list)
Argument 2
[
4,
8
]Returns
[
[
4,
8
]
]Constraints
[1, 5] and [5, 10] share the minute 5, so they merge into [1, 10].new has end < start, it is bad data: return a copy of windows unchanged.windows and a valid new, return a list holding just new.Worked example
Take [[1, 3], [6, 9], [12, 15]] and the new window [2, 7]. [1, 3] ends at 3, which is not before 2, so it overlaps: the new window becomes [1, 7]. [6, 9] starts at 6, which is not after 7, so it overlaps too: the window becomes [1, 9]. [12, 15] starts at 12, after 9, so it stays. The answer is [[1, 9], [12, 15]].
The trap: for [[1, 5], [10, 12]] and the new window [5, 10], all three ranges share an endpoint. A check written with strict < and > treats them as separate and leaves three windows. Because ranges are closed they must become the single window [1, 12].
What this tests
The insert interval pattern, LeetCode 'Insert Interval'. Because the list is already sorted, the work has three phases. Copy what is entirely before the new window, absorb what touches it, and copy what is entirely after. Data engineers do this when a schedule or a set of processed ranges has to stay tidy as new entries arrive.
insert_window(windows: list[list[int]], new: list[int]) -> list[list[int]]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.