Skip to main content

986. Interval List Intersections

mediumAsked at Meta

Short answer

  • Best approach: Two-pointer sweep. Two pointers i and j into the lists. At each step, compute the overlap of firstList[i] and secondList[j] (if any). Advance whichever interval ends first.
  • Time O(m + n), space O(m + n) for output
  • How often Meta asks it: frequently asked · Verified

Given two sorted lists of disjoint intervals, return the intersection. Meta asks this to test whether you reach for the two-pointer pattern and articulate why it's O(m+n) instead of O(m*n).

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Meta loops.

  • Glassdoor (2026-Q1)— Meta E3/E4 onsite reports cite this as a recurring intervals problem.
  • Blind (2025-12)— Recurring Meta interview question.

Problem

You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [start_i, end_i] and secondList[j] = [start_j, end_j]. Each list of intervals is pairwise disjoint and in sorted order. Return the intersection of these two interval lists.

Constraints

  • 0 <= firstList.length, secondList.length <= 1000
  • firstList.length + secondList.length >= 1
  • 0 <= start_i < end_i <= 10^9
  • end_i < start_(i+1)
  • 0 <= start_j < end_j <= 10^9
  • end_j < start_(j+1)

Examples

Example 1

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

Example 2

Input
firstList = [[1,3],[5,9]], secondList = []
Output
[]

Approaches

1. Two-pointer sweep (optimal)

Two pointers i and j into the lists. At each step, compute the overlap of firstList[i] and secondList[j] (if any). Advance whichever interval ends first.

Time
O(m + n)
Space
O(m + n) for output
function intervalIntersection(firstList, secondList) {
  const result = [];
  let i = 0, j = 0;
  while (i < firstList.length && j < secondList.length) {
    const lo = Math.max(firstList[i][0], secondList[j][0]);
    const hi = Math.min(firstList[i][1], secondList[j][1]);
    if (lo <= hi) result.push([lo, hi]);
    if (firstList[i][1] < secondList[j][1]) i++;
    else j++;
  }
  return result;
}

Tradeoff: Linear in the total length. Advancing whichever interval ends first is correct because once an interval is exhausted, it can't intersect any further intervals in the other list.

Meta-specific tips

Meta interviewers grade this on whether you articulate why the two-pointer approach is correct. State out loud: 'I advance the interval that ENDS first because it can't possibly intersect anything later — the other list's intervals are sorted by start.' Without that articulation, the algorithm looks like magic.

Common mistakes

  • Computing the overlap as (lo, hi) without checking lo <= hi — produces invalid intervals when there's no overlap.
  • Advancing both pointers (i++; j++;) on every iteration — skips valid intersections.
  • Using strict inequality (<) instead of <= for the endpoint match — misses single-point intersections like [5,5].

Follow-up questions

An interviewer at Meta may pivot to one of these next:

  • What if the lists weren't sorted?
  • Merge Intervals (LC 56) — different problem class, also intervals.
  • Meeting Rooms II (LC 253) — interval scheduling.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Ctrl+Enter to execute

FAQ

Why advance the one that ends first?

If interval A ends before interval B, A cannot overlap with any future interval in the other list (they're all later). So we move past A.

What if intervals were half-open instead of closed?

Replace <= with <. Closed intervals require <= because [5,5] is a valid one-point intersection.

Originally published on InterviewChamp.AI