Magicsheet logo

Merge Intervals

Medium
55.3%
Updated 6/1/2025

Merge Intervals

What is this problem about?

The "Merge Intervals" coding problem is a classic algorithmic challenge that asks you to combine overlapping intervals into a single, consolidated set of non-overlapping intervals. You are typically given an array or list of intervals, where each interval is represented by a pair of numbers, [start, end]. The goal is to produce a new list where any intervals that overlap are merged into a single, larger interval. For example, if you have [1,3] and [2,6], they merge to [1,6]. This problem is fundamental for understanding how to process and simplify ranges of data, and it's a very common Array interview question that often appears alongside Sorting interview pattern questions.

Why is this asked in interviews?

This "Merge Intervals" interview question is incredibly popular among top tech companies like Apple, Google, Amazon, and Microsoft for several reasons. It tests a candidate's ability to:

  1. Handle and process array data: Demonstrates proficiency in iterating and manipulating arrays.
  2. Apply sorting algorithms: A crucial first step for an efficient solution involves sorting, showcasing knowledge of sorting's impact on algorithm design.
  3. Logical reasoning and edge cases: Candidates must carefully consider various overlap scenarios (partial overlap, one interval fully containing another, no overlap) and manage the boundaries correctly.
  4. Problem decomposition: The task requires breaking down the problem into sorting, then iterating and merging based on logical conditions. It’s an excellent way to evaluate a candidate’s structured thinking, attention to detail, and ability to optimize solutions.

Algorithmic pattern used

The primary algorithmic pattern used to solve the "Merge Intervals" coding problem effectively is Greedy Approach combined with Sorting.

  1. Sorting: The crucial first step is to sort the intervals based on their start times. This ensures that when you iterate through the intervals, you are always considering them in an increasing order of their starting points. Without sorting, determining overlaps becomes significantly more complex and inefficient.
  2. Greedy Approach: After sorting, you can iterate through the sorted intervals and apply a greedy strategy. Maintain a merged list (or array) and a current_merged_interval. For each new interval, compare its start time with the current_merged_interval's end time.
    • If there's an overlap (current interval's end is greater than or equal to the new interval's start), merge them by updating the current_merged_interval's end to be the maximum of its current end and the new interval's end.
    • If there's no overlap, the current_merged_interval is complete, so add it to the merged list and start a new current_merged_interval with the new interval. This greedy choice at each step (merging if possible, otherwise starting a new interval) leads to the optimal global solution.

Example explanation

Let's say we are given the following intervals: [[1,3], [8,10], [2,6], [15,18]].

  1. Sort the intervals by their start times: [[1,3], [2,6], [8,10], [15,18]]

  2. Initialize a merged_intervals list and add the first interval: merged_intervals = [[1,3]] current_merged = [1,3]

  3. Iterate through the rest of the sorted intervals:

    • Next interval: [2,6]

      • current_merged's end (3) is greater than or equal to [2,6]'s start (2). They overlap.
      • Merge: Update current_merged's end to max(3, 6) = 6.
      • current_merged is now [1,6]. merged_intervals is still [[1,3]] (we'll update it later, or overwrite the last element).
    • Next interval: [8,10]

      • current_merged's end (6) is less than [8,10]'s start (8). No overlap.
      • Add current_merged ([1,6]) to merged_intervals. merged_intervals = [[1,6]]
      • Start a new current_merged: [8,10].
    • Next interval: [15,18]

      • current_merged's end (10) is less than [15,18]'s start (15). No overlap.
      • Add current_merged ([8,10]) to merged_intervals. merged_intervals = [[1,6], [8,10]]
      • Start a new current_merged: [15,18].
  4. After iterating through all intervals, add the last current_merged: merged_intervals = [[1,6], [8,10], [15,18]]

This is the final set of non-overlapping intervals.

Common mistakes candidates make

A very common mistake is to forget to sort the intervals initially. Without sorting, the greedy approach fails, as you might miss overlaps with intervals that start later but overlap with earlier ones. Another frequent error is incorrectly updating the end boundary of the merged interval, sometimes simply taking the new interval's end without comparing it to the current merged end, or failing to update the merged list correctly. Edge cases like an empty input list, a list with only one interval, or intervals that are adjacent but not overlapping ([1,2], [2,3]) can also trip up candidates if the comparison logic is not precise (current.end >= next.start vs. current.end > next.start).

Interview preparation tip

For the "Merge Intervals" coding problem, ensure you have a solid understanding of sorting algorithms (especially how to sort custom objects or pairs). Practice implementing the sorting step and then the greedy merging logic carefully. Draw out multiple examples with different overlap scenarios (full containment, partial overlap, no overlap, adjacent intervals) to solidify your understanding. Pay close attention to the conditions for merging and when to start a new merged interval. This problem is an excellent way to practice Array manipulation and applying a Sorting strategy to simplify subsequent operations. Thinking about the time and space complexity (dominated by sorting) will also be beneficial.

Similar Questions