FastPrepMerge Overlapping Intervals

Merge Overlapping Intervals

ByteDance logoByteDance● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given closed integer intervals [start, end], merge every overlapping group into one interval. Intervals sharing an endpoint overlap.

Return the merged intervals in increasing start order. An empty input returns an empty list. For this exercise, use the conventional closed-interval interpretation and the bounded array interface below.

Function

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

Examples

Example 1

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

The first two intervals overlap.

Example 2

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

Endpoint contact is included for closed intervals.

Example 3

intervals = []return = []

No intervals need to be merged.

Constraints

  • 0 <= intervals.length <= 2000.
  • Every row contains two integers with -10^6 <= start <= end <= 10^6.

More ByteDance problems

See ByteDance hiring insights
public int[][] mergeIntervals(int[][] intervals) {
    // Write your code here.
}
intervals[[1,3],[2,6],[8,10],[15,18]]
expected[[1,6],[8,10],[15,18]]
Checking account…