The Insert Interval interview question is an interval merging task. You are given a list of non-overlapping intervals sorted by start time. You need to insert a newInterval into the list, merging it with any existing intervals that overlap with it, and ensuring the final list remains sorted and non-overlapping.
This is a standard question at Apple, Amazon, and Uber. It tests your ability to handle Array interview patterns involving ranges and overlaps. Unlike "Merge Intervals," where you sort everything first, this problem expects you to leverage the existing sorted property to achieve a linear solution.
This problem uses a Three-Phase Linear Scan.
newInterval's start to min(starts) and end to max(ends).Intervals: [[1, 3], [6, 9]], New: [2, 5]
[1, 3] overlaps with [2, 5] because .
[min(1, 2), max(3, 5)] = [1, 5].[6, 9] starts after [1, 5] ends.
[[1, 5], [6, 9]].sort() on the whole array () instead of doing a linear pass ().Interval problems are all about boundary comparisons. Always check curr.end < new.start (left), curr.start > new.end (right), and the everything else case (overlap). This systematic approach prevents logic bugs in Line Sweep interview patterns.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Value of an Ordered Triplet II | Medium | Solve | |
| Non-decreasing Array | Medium | Solve | |
| Number of Adjacent Elements With the Same Color | Medium | Solve | |
| Maximize Distance to Closest Person | Medium | Solve | |
| All Divisions With the Highest Score of a Binary Array | Medium | Solve |