Practice: Merge Intervals

Problem: https://leetcode.com/problems/merge-intervals/

Recognition reminder: the input is a list of [start, end] pairs and the question is to combine the ones that overlap. That is the base case of the interval pattern. The lesson is the two-move discipline: sort by start first, then sweep left to right, deciding for each interval whether it extends the one you are building or begins a new one. The boundary detail (does a touching pair like [1, 4] and [4, 5] count as overlapping?) is the part that decides correctness, so settle it from the problem statement.

Before you start (the five-beat rhythm)

  1. Your Pattern Cards for this week are already written (tries and intervals).
  2. Name the pattern aloud and write your approach as a plain-English comment before any code.
  3. Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
  4. If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
  5. Debrief in your commit message before moving to the next problem.

Your target

Fill these in yourself before you look at anyone else’s solution:

Target time complexity:  ____
Target space complexity: ____

Ask yourself what dominates the running time once you commit to sorting first. Then ask why the sweep only ever has to compare the current interval against the single most recent one you kept, and not against everything before it. That second question is the reason the sort is worth its cost.

The function you implement

Write a function merge in a file named solution.py in this folder (your own work repo, not the course repo):

merge(intervals: list[list[int]]) -> list[list[int]]

Return the list of merged, non-overlapping intervals, sorted by start. For example, [[1,3],[2,6],[8,10],[15,18]] returns [[1,6],[8,10],[15,18]], and [[1,4],[4,5]] returns [[1,5]].

Where your code goes

Write your solution in your own work repo (see getting-started.md), not in this folder. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.

Debrief (paste into your commit message)

1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?