Skip to main content

56. Merge Intervals

mediumAsked at Juniper Networks

Merge overlapping intervals into a minimal set. Juniper asks this because merging overlapping prefix ranges is a real task in IP route aggregation — combining contiguous or overlapping CIDR blocks into a shorter routing table is exactly this algorithm applied to network address space.

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

Source citations

Public interview reports confirming this problem appears in Juniper Networks loops.

  • Glassdoor (2026-Q1)Cited in Juniper SWE onsite reports as a frequent interval-manipulation problem with networking context.
  • Blind (2025-12)Juniper candidates list merge-intervals as a medium-difficulty problem that appears across routing and control-plane teams.

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 at 2-3, merged 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. Sort by start + linear merge

Sort intervals by start time. Walk through: if the current interval overlaps the last merged interval (start <= last.end), extend the end. Otherwise push a new interval.

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]) {
      // overlapping: extend the end if necessary
      last[1] = Math.max(last[1], curr[1]);
    } else {
      result.push(curr);
    }
  }
  return result;
}

Tradeoff: O(n log n) dominated by the sort. The merge pass is O(n). This is the canonical solution — optimal for comparison-based sorting.

Juniper Networks-specific tips

Draw the parallel to IP route aggregation explicitly: 'If I have BGP prefixes 10.0.0.0/24 and 10.0.1.0/24 arriving in arbitrary order, sorting by prefix address and then merging contiguous ranges is exactly this algorithm.' Juniper interviewers strongly appreciate domain-relevant examples. Make sure you handle the case where curr.end < last.end (the current interval is entirely contained) — use Math.max(last.end, curr.end) not just curr.end.

Common mistakes

  • Forgetting to sort before iterating — the merge only works if intervals are ordered by start.
  • Using last[1] = curr[1] instead of Math.max(last[1], curr[1]) — fails when the current interval is entirely contained within the previous one.
  • Checking curr[0] < last[1] (strict) instead of <= — touching intervals [1,4] and [4,5] should be merged.
  • Not handling the single-interval edge case — intervals[0] as the seed handles it correctly.

Follow-up questions

An interviewer at Juniper Networks may pivot to one of these next:

  • Insert Interval (LC 57) — insert a new interval into a sorted non-overlapping list.
  • Non-overlapping Intervals (LC 435) — find the minimum number of intervals to remove.
  • How would you merge overlapping CIDR prefix ranges in a BGP routing table?

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 time?

After sorting, any interval that overlaps the previous one must have a start <= the previous interval's end. This invariant lets us make a single linear pass.

Why use Math.max for the end?

A new interval could be entirely contained within the current merged interval. For example, [1,10] followed by [2,4] — the end should stay at 10, not shrink to 4.

What is the time complexity?

O(n log n) for the sort and O(n) for the merge pass. The bottleneck is the sort.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →