Algorithms and Data Structures · Middle · Question 20 of 75

How do you merge overlapping intervals like [[1,3],[2,6],[8,10]]?

Explanation

After sorting by start, an interval overlaps the previous one exactly when its start is not after the previous end. One pass gives [[1,6],[8,10]]. The total cost is O(n log n) for the sort.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 20

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz