Merge an Array With Overlapping Intervals

Merge overlapping intervals in an array of interval pairs.

Statement

We’re given an array of interval pairs as input where each interval has a start and end timestamp. The input array is sorted by starting timestamps. Merge the overlapping intervals and return a new output array.

Example

Consider the input array below. Intervals [1, 5], [3, 7], [4, 6], [6, 8] are overlapping, so they should be merged to one big interval [1, 8]. Similarly, intervals [10, 12] and [12, 15] are also overlapping and should be merged to [10, 15].

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