Problem · Array

Merge Intervals

Learn this problem
MediumBloomberg logoBloombergFULLTIMEPHONE SCREEN

Problem statement

Given an array intervals where intervals[i] = [start_i, end_i], merge every pair of overlapping closed intervals. Return the non-overlapping intervals that cover exactly the same values, ordered by increasing start.

Function

merge(intervals: int[][]) → int[][]

Examples

Example 1

intervals = [[1,3],[2,6],[8,10],[15,18]]return = [[1,6],[8,10],[15,18]]

The closed intervals [1,3] and [2,6] overlap, so they merge into [1,6].

Example 2

intervals = [[1,4],[4,5]]return = [[1,5]]

Closed intervals that share an endpoint overlap.

Constraints

  • 1 <= intervals.length <= 10^4
  • intervals[i].length = 2
  • 0 <= start_i <= end_i <= 10^4

More Bloomberg problems

drafts saved locally
public int[][] merge(int[][] intervals) {
    // write your code here
}
intervals[[1,3],[2,6],[8,10],[15,18]]
expected[[1,6],[8,10],[15,18]]
checking account