Insert Interval
Medium Intervals AI round
intervals is a list of intervals, each [start, end] with start ≤ end, holding every point from its start to its end, both ends included. The intervals are in order of their starts, and no two of them overlap or even touch: each one starts after the one before it ends.
Put newInterval, also [start, end], among them. Where it overlaps intervals of the list, even at a single end point, join it and them into one interval, from the smallest start to the largest end.
Return the new list of intervals, still in order of their starts, with no two of them overlapping.
Example 1
[8, 12] overlaps [7, 9] and [11, 13], so the three join into [7, 13]. [2, 4] ends before it starts and [16, 20] starts after it ends, so they stay as they are.
Example 2
[5, 10] touches [1, 5] at 5 and [10, 14] at 10. A shared end point is enough, so all three join into [1, 14].
Example 3
Constraints
0 ≤ intervals.length ≤ 10⁵intervals[i] = [start, end]andnewInterval = [start, end], with0 ≤ start ≤ end ≤ 10⁹- Each interval in
intervalsstarts after the one before it ends
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Intervals
- Merge IntervalsAI round · Medium
- Non-overlapping IntervalsAI round · Medium
- Meeting Rooms IIMedium
- The Skyline ProblemHard
- Interval List IntersectionsMedium