FastPrepMerge K Sorted Interval Lists

Merge K Sorted Interval Lists

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

You are given intervalLists, an array of interval lists. Every interval is a closed pair [start, end].

Within each list, intervals are sorted by increasing start and do not overlap. Merge all intervals from all lists so that every pair that overlaps or shares an endpoint belongs to one output interval.

Return the merged, non-overlapping intervals sorted by increasing start.

Function

mergeKIntervalLists(intervalLists: int[][][]) → int[][]

Examples

Example 1

intervalLists = [[[1,3],[7,9]],[[2,5],[10,12]],[[6,8]]]return = [[1,5],[6,9],[10,12]]

The first two lists merge [1,3] with [2,5]. The intervals [6,8] and [7,9] form the second merged interval.

Example 2

intervalLists = [[],[[1,4]],[[4,6]]]return = [[1,6]]

The empty list contributes nothing. The other intervals share endpoint 4, so their closed ranges merge.

Example 3

intervalLists = [[[-5,-3],[0,2]],[[3,4]],[[8,10]]]return = [[-5,-3],[0,2],[3,4],[8,10]]

No intervals overlap or share an endpoint, so all four remain separate and sorted.

Constraints

  • 1 <= intervalLists.length <= 10000.
  • The total number of intervals across all lists is between 1 and 100000.
  • Every interval contains exactly two integers [start, end] with -10^9 <= start <= end <= 10^9.
  • Each individual list is sorted by increasing start and contains no overlapping intervals.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[][] mergeKIntervalLists(int[][][] intervalLists) {
    // Write your code here.
}
intervalLists[[[1,3],[7,9]],[[2,5],[10,12]],[[6,8]]]
expected[[1,5],[6,9],[10,12]]
Checking account…