Skip to main content

56. Merge Intervals

mediumAsked at Pinterest

Merge Intervals is Pinterest's canonical interval-handling problem: given an array of [start, end] intervals, merge all overlapping ones and return the result. The interviewer wants the sort-then-sweep pattern, not nested loops.

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

Source citations

Public interview reports confirming this problem appears in Pinterest loops.

  • Glassdoor (2026-Q1)Pinterest SWE onsite reports list Merge Intervals as the array-manipulation round for the backend / infra track.
  • LeetCode Pinterest tag (2026-Q1)Top-tier frequency on the Pinterest company-tagged problem list.
  • Reddit r/cscareerquestions (2025-12)Pinterest new-grad phone screen reports include this as a classic interval round.

Problem

Given an array of intervals where intervals[i] = [start_i, end_i], 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 <= start_i <= end_i <= 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 them into [1,6].

Example 2

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

Explanation: Intervals touching at the boundary count as overlapping.

Approaches

1. Brute force: pairwise merge until stable

Repeatedly scan every pair; if any two overlap, merge them and restart. Stop when a full scan produces no merges.

Time
O(n^3) worst case
Space
O(n)
function mergeBrute(intervals) {
  const result = intervals.map((x) => x.slice());
  let changed = true;
  while (changed) {
    changed = false;
    outer: for (let i = 0; i < result.length; i++) {
      for (let j = i + 1; j < result.length; j++) {
        if (overlap(result[i], result[j])) {
          result[i] = [Math.min(result[i][0], result[j][0]), Math.max(result[i][1], result[j][1])];
          result.splice(j, 1);
          changed = true;
          break outer;
        }
      }
    }
  }
  return result;
  function overlap(a, b) { return a[0] <= b[1] && b[0] <= a[1]; }
}

Tradeoff: Correct but cubic; never the final answer at Pinterest. Mention this for completeness then move on.

2. Sort by start, then sweep (optimal)

Sort intervals by start. Walk left-to-right, merging into the last interval in the result if the current one overlaps, else push.

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

Tradeoff: O(n log n) for the sort + O(n) sweep. The sort is the dominant cost. After sorting, a single pass suffices because once we move past an interval, we never need to revisit it.

Pinterest-specific tips

Pinterest interviewers want you to volunteer the boundary-touching edge case ('[1,4] and [4,5] — does touching count as overlapping?') and pick one cleanly before coding. They also like the sweep-line follow-up: 'what if I gave you a stream of intervals and asked for the current merged set at any time?' — that's a TreeMap problem, signal you know it exists.

Common mistakes

  • Sorting by end instead of start — that's the meeting-rooms / activity-selection pattern, different problem.
  • Not handling touching intervals: [1,4] and [4,5] should merge into [1,5].
  • Using <= vs < inconsistently between the sort and the overlap check.
  • Mutating the input array — copy intervals[i] before pushing to result.

Follow-up questions

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

  • Insert Interval (LeetCode 57): given a sorted-merged list, insert a new interval.
  • Sweep-line: what's the maximum number of overlapping intervals at any point?
  • Stream of intervals: maintain merged state with each new arrival (TreeMap / sorted set).
  • Meeting Rooms II: minimum number of conference rooms to host all meetings.

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 does Pinterest care about interval problems?

Calendar / scheduling / availability features and ads-pacing windows all reduce to interval merging or sweep-line. The pattern shows up across multiple Pinterest product surfaces.

Can I merge in-place to save space?

Yes, sort then merge using two pointers (write index trailing read index). Most interviewers accept O(n) extra space because the output is necessarily O(n) anyway.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →