Skip to main content

56. Merge Intervals

mediumAsked at Bloomberg

Given an array of intervals, merge all overlapping intervals. Bloomberg uses this to test the sort-then-sweep pattern that shows up in calendar, market-hours, and time-window problems across their data-infrastructure teams.

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

Source citations

Public interview reports confirming this problem appears in Bloomberg loops.

  • Glassdoor (2026-Q1)Bloomberg SWE onsite reports list Merge Intervals as a near-default interval question.
  • Blind (2025-12)Bloomberg new-grad reports cite this for finance + scheduling teams.

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: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].

Example 2

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

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

Approaches

1. Sort by start, then linear sweep

Sort intervals by start. Walk the list; if the current overlaps the last merged, extend its end. Else append.

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

Tradeoff: The canonical approach. O(n log n) dominated by the sort. Bloomberg interviewers specifically grade whether you push the FIRST interval before the loop — that removes the initial special case.

2. Sweep line with event counting

Treat each interval as +1 at start, -1 at end+1. Sort events; merge while count > 0.

Time
O(n log n)
Space
O(n)
function mergeSweep(intervals) {
  const events = [];
  for (const [s, e] of intervals) {
    events.push([s, 1]);
    events.push([e, -1]);
  }
  events.sort((a, b) => a[0] - b[0] || b[1] - a[1]);
  const result = [];
  let count = 0, startOfRun = null;
  for (const [t, delta] of events) {
    if (count === 0 && delta === 1) startOfRun = t;
    count += delta;
    if (count === 0) result.push([startOfRun, t]);
  }
  return result;
}

Tradeoff: Generalizes to harder interval problems (meeting rooms, max concurrent). Mention as a follow-up pattern.

Bloomberg-specific tips

Bloomberg interviewers specifically watch for the SORT step + the in-place extend-or-append loop. Touch-edge intervals ([1,4] and [4,5]) overlap by Bloomberg's definition — use <= not <.

Common mistakes

  • Using < instead of <= for the overlap check — touch-edge intervals must merge.
  • Forgetting to handle the empty-intervals input.
  • Comparing intervals[i] directly with intervals[i-1] instead of result[-1] — fails when many overlapping intervals chain into one.

Follow-up questions

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

  • Insert Interval (LC 57) — pre-sorted, no need to re-sort.
  • Non-overlapping Intervals (LC 435) — sort by end, count removals.
  • Meeting Rooms II (LC 253) — count max concurrent intervals.

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?

Sorting by start guarantees that if intervals[i] doesn't overlap result[-1], no later interval will either (they all start at intervals[i] or later).

Do touch-edge intervals merge?

Per LeetCode's definition: yes ([1,4] and [4,5] merge into [1,5]). Bloomberg's onsite version sometimes specifies otherwise — clarify with the interviewer first.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →