Problem · Array

Intersect Two Sorted Interval Lists

Learn this problem
MediumByteDance logoByteDanceFULLTIMEPHONE SCREEN

Problem statement

You are given two lists of closed integer intervals, firstList and secondList. Within each list, intervals are sorted by start and pairwise disjoint.

Return every nonempty intersection between one interval from firstList and one interval from secondList, ordered by increasing start. A shared endpoint is a valid one-point intersection.

Function

intersectIntervalLists(firstList: int[][], secondList: int[][]) → int[][]

Examples

Example 1

firstList = [[0,2],[5,10],[13,23],[24,25]]secondList = [[1,5],[8,12],[15,24],[25,26]]return = [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]

Each result is the closed overlap of the current intervals. Endpoint-only overlaps such as [5,5] are retained.

Example 2

firstList = [[1,3],[6,8]]secondList = [[4,5],[9,10]]return = []

No interval in one list overlaps an interval in the other.

Example 3

firstList = []secondList = [[2,7]]return = []

An empty list has no intersections.

Constraints

  • 0 <= firstList.length, secondList.length <= 100000.
  • Every interval has exactly two signed 32-bit endpoints [start, end] with start <= end.
  • Within each list, intervals are sorted by start and satisfy intervals[i][1] < intervals[i + 1][0].

More ByteDance problems

drafts saved locally
public int[][] intersectIntervalLists(int[][] firstList, int[][] secondList) {
  // write your code here
}
firstList[[0,2],[5,10],[13,23],[24,25]]
secondList[[1,5],[8,12],[15,24],[25,26]]
expected[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
checking account