DIY: Merge Intervals
Solve the interview question "Merge Intervals" yourself in this lesson.
We'll cover the following
Problem statement
You are given an array of intervals in the form of start and end times. Your task is to merge as many intervals as possible. Two intervals can be merged if they are overlapping (one begins before the other ends) or adjacent (one starts exactly when the other ends). The output is the resultant array of intervals.
Input
The input is an array of arrays. The nested arrays contain two integers representing the starting and ending points of the interval. The following is an example input:
[[1, 4], [2, 5], [6, 7], [7, 10], [11, 12]]
Output
The output is the array of merged intervals. The following is an example output:
[[1, 5], [6, 10], [11, 12]]
Coding exercise
You need to implement the function mergeIntervals(intervals)
, where intervals
is the array of intervals. The function returns an array of intervals representing the merged intervals.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.