DIY: Insert Interval

Solve the interview question "Insert Interval" yourself in this lesson.

We'll cover the following

Problem statement

For this problem, you are given an array of non-overlapping intervals, and you need to insert another interval into the array. The output should contain an array of mutually exclusive intervals.

Input

The function has two inputs. The first input is an array of arrays representing the array of intervals. The nested arrays contain two integers representing the starting and ending time of the intervals. The second input is an array of two integers representing the new interval to be inserted. The following is an example input:

[[1, 3], [4, 5], [7, 9], [9, 15], [10, 14]] 
[2, 8]

Output

The output is the array of intervals after inserting the new interval. The following is an example output:

[[1, 9], [9, 15], [10, 14]]

Coding exercise

You need to implement the function insertInterval(intervals, newInterval), where intervals is the array of intervals and newInterval is the interval to be inserted. The function returns an array of intervals after the insertion of the new interval.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.