CodePark/ problems / Merge Overlapping Ranges
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Sorting and intervals

Merge Overlapping Ranges

mediumabout 20 minutes in an interview

You get a list of ranges [start, end], with start <= end. Merge every group of ranges that overlap, and return the result. The result must be the fewest ranges that cover exactly the same numbers.

  • Ranges that touch count as overlapping. For example, [1, 4] and [4, 6] merge into [1, 6].
  • The input comes in no particular order. Return the merged ranges sorted by start.
  • The input can be empty (return []).
  • The input can hold negative numbers, and up to 100,000 ranges.
Input: intervals = [[8, 10], [1, 4], [3, 6], [12, 15]] Output: [[1, 6], [8, 10], [12, 15]][1, 4] and [3, 6] share the numbers 3 to 4, so they become [1, 6].
Input: intervals = [[1, 4], [4, 6]] Output: [[1, 6]]