Insert Interval Problem
Insert Interval Problem — ExecCode Medium DSA Practice
Solve the Insert Interval problem on ExecCode. Free online medium DSA practice in Intervals. Write and run code in Java, C++, Python — no signup required to run.
Problem description
You are given an array of non-overlapping intervals sorted by start time, and a new interval to insert. Insert the new interval into the array, merging any intervals that now overlap, and return the resulting array still sorted by start time.
Examples
Input {"intervals": [[1, 3], [6, 9]], "newInterval": [2, 5]}; Output [[1, 5], [6, 9]]. Input {"intervals": [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], "newInterval": [4, 8]}; Output [[1, 2], [3, 10], [12, 16]]. Input {"intervals": [], "newInterval": [5, 7]}; Output [[5, 7]]
Constraints
0 ≤ intervals.length ≤ 10⁴ intervals[i].length == 2 0 ≤ starti ≤ endi ≤ 10⁵ intervals sorted by starti, non-overlapping newInterval.length == 2 0 ≤ start ≤ end ≤ 10⁵
Practice Insert Interval free on ExecCode. Browse DSA problems, topic map, and placement guides.