mediumIntervalsGoogleLinkedIn
Calendar Hole Patcher
A scheduler inserts a new busy block into a sorted calendar of non-overlapping blocks, merging any overlaps.
Given a sorted list of non-overlapping intervals and a new interval, insert it merging overlaps and return the resulting intervals.
Choose approach
1/3
Narrationstep 1 of 3
I0 overlaps NEW -> expand NEW to [1,5].
Variables
i=0
newStart=1
newEnd=5
How to think about it
- Three cases per interval: before, overlap, after.
- Expand NEW while any overlap persists.
Edge cases to catch in interview
- Empty intervals -> just [newInterval].
- NEW overlaps all -> single merged interval.
Examples
Example 1
[[1,2],[3,10],[12,16]]{"intervals":[[1,2],[3,5],[6,7],[8,10],[12,16]],"newInterval":[4,8]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed