Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.merge(intervals). Return sorted, non-overlapping intervals covering exactly the same ranges. Intervals that share an endpoint overlap.

Starter code

class Solution:
    def merge(self, intervals):
        pass
Test cases

overlapping-ranges

{
  "args": [
    [
      [
        1,
        3
      ],
      [
        2,
        6
      ],
      [
        8,
        10
      ],
      [
        15,
        18
      ]
    ]
  ]
}

Expected: [[1,6],[8,10],[15,18]]

touching-endpoints

{
  "args": [
    [
      [
        1,
        4
      ],
      [
        4,
        5
      ]
    ]
  ]
}

Expected: [[1,5]]

Wizard outline
  1. Step 1: Initialize Solution.merge

    Replace the empty starter with the first real state owned by Solution.merge. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Overlapping Ranges case

    Complete the readable core algorithm for one representative Interview case. Apply the interval merge invariant.

  4. Step 4: Harden the Touching Endpoints boundary

    Repair the reviewed boundary and pass the complete submission contract. After sorting, every interval that can overlap the active range is adjacent to it. Extending on overlap and starting a new range only on a larger start preserves exactly the union of all processed intervals.

Footguns and prerequisites
  • Touching endpoints overlap under this contract.
  • arrays strings two pointers sliding window
Reviewed references
Practice prerequisites
  • Merge Two Ordered Runs(opens in a new tab)

    Merge Two Ordered Runs isolates the output is sorted and contains exactly the consumed prefixes; the two pointers identify the smallest unconsumed candidates. That focused state discipline is required when implementing merge intervals as a complete Interview Problem.

Recommended approach and implementation

Sort by start, then either append a disjoint interval or extend the active merged endpoint.

Why it works: After sorting, every interval that can overlap the active range is adjacent to it. Extending on overlap and starting a new range only on a larger start preserves exactly the union of all processed intervals.

class Solution:
    def merge(self, intervals):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        merged = []
        for start, end in sorted(intervals):
            if not merged or start > merged[-1][1]:
                merged.append([start, end])
            else:
                merged[-1][1] = max(merged[-1][1], end)
        return merged