Skip to main content

19. Merge Intervals

mediumAsked at Adobe

Given a collection of intervals, merge all overlapping intervals. Adobe uses interval merging extensively in creative applications — timeline editing in Premiere Pro, selection ranges in Photoshop, and layer time spans all require efficient overlap detection and merging.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Adobe loops.

  • Glassdoor (2026-02)Adobe senior candidate reports interval merging as an onsite question tied to timeline editing domains.
  • Blind (2025-11)Adobe SDE-II and principal candidates report interval problems across multiple rounds.

Problem

Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals and return an array of the non-overlapping intervals that cover all the intervals in the input.

Constraints

  • 1 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 10^4

Examples

Example 1

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

Explanation: [1,3] and [2,6] overlap, merge to [1,6].

Example 2

Input
intervals = [[1,4],[4,5]]
Output
[[1,5]]

Explanation: Touching intervals [1,4] and [4,5] are considered overlapping.

Approaches

1. Brute force pairwise comparison

Repeatedly scan all pairs of intervals for overlaps and merge until no overlaps remain.

Time
O(n^2)
Space
O(n)
function merge(intervals) {
  let merged = [...intervals];
  let changed = true;
  while (changed) {
    changed = false;
    const next = [];
    let used = Array(merged.length).fill(false);
    for (let i = 0; i < merged.length; i++) {
      if (used[i]) continue;
      let [s, e] = merged[i];
      for (let j = i+1; j < merged.length; j++) {
        if (!used[j] && merged[j][0] <= e) {
          e = Math.max(e, merged[j][1]);
          used[j] = true;
          changed = true;
        }
      }
      next.push([s, e]);
    }
    merged = next;
  }
  return merged;
}

Tradeoff: Correct but O(n^2) or worse; Adobe expects the sort-then-scan approach immediately.

2. Sort by start, single-pass merge

Sort intervals by start time. Then scan left to right: if the current interval's start is <= the last merged interval's end, extend the last interval's end. Otherwise, push the current interval as a new group. Sorting guarantees all candidates for merging are adjacent.

Time
O(n log n)
Space
O(n)
function merge(intervals) {
  intervals.sort((a, b) => a[0] - b[0]);
  const result = [intervals[0]];
  for (let i = 1; i < intervals.length; i++) {
    const last = result[result.length - 1];
    const curr = intervals[i];
    if (curr[0] <= last[1]) {
      last[1] = Math.max(last[1], curr[1]);
    } else {
      result.push(curr);
    }
  }
  return result;
}

Tradeoff: O(n log n) dominated by sorting; O(n) space for output. The key insight: after sorting by start, any interval that cannot merge with the current tail cannot merge with any future interval either.

Adobe-specific tips

Adobe interviewers specifically appreciate candidates who connect this to Premiere Pro's clip-overlap detection or Photoshop's selection history merge. Mention that sorting by start time is the canonical preprocessing step for all interval problems. Be ready for Insert Interval (LC 57) as an immediate follow-up — inserting a new interval into an already-sorted, non-overlapping list.

Common mistakes

  • Forgetting to sort the input — the algorithm only works correctly on sorted intervals.
  • Using < instead of <= when checking overlap — touching intervals ([1,4] and [4,5]) should merge.
  • Mutating intervals[0] before copying to result — sort changes the array in place, so take care with index 0 initialization.

Follow-up questions

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

  • Insert Interval (LC 57): insert a new interval into a sorted non-overlapping list and merge.
  • Meeting Rooms (LC 252): determine if a person can attend all meetings given intervals.
  • How would you merge intervals in a streaming fashion where new intervals arrive continuously?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why sort by start and not by end?

Sorting by start ensures that any interval that could overlap with the current tail is immediately adjacent. Sorting by end would require checking all earlier intervals for overlap.

What if the input is already sorted?

Skip the sort step — O(n) time. Always mention this optimization; it shows you understand the sort is the bottleneck.

Practice these live with InterviewChamp.AI

Drill Merge Intervals and other Adobe interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →