Practice: Insert Interval
Problem: https://leetcode.com/problems/insert-interval/
Recognition reminder: you are handed an already-sorted, already-merged list of [start, end] intervals and one new interval to place. The input shape (intervals plus an overlap question) is the interval pattern again, but the twist is that the list is already sorted, so you do not re-sort. The lesson is the three-phase sweep: copy the intervals that end before the new one starts, absorb every interval that overlaps the new one by widening it, then copy the rest.
Before you start (the five-beat rhythm)
- Your Pattern Cards for this week are already written (tries and intervals).
- Name the pattern aloud and write your approach as a plain-English comment before any code.
- Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
- If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
- 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: ____
The input is already sorted, which is a gift: ask what that lets you skip compared to Merge Intervals, and what your running time can be as a result. Then think about the three phases (before, overlapping, after) and what the overlap test is for the middle phase.
The function you implement
Write a function insert in a file named solution.py in this folder (your own work repo, not the course repo):
insert(intervals: list[list[int]], new_interval: list[int]) -> list[list[int]]
intervals is sorted by start and has no internal overlaps. Insert new_interval, merging as needed, and return the result still sorted and non-overlapping. For example, inserting [2,5] into [[1,3],[6,9]] returns [[1,5],[6,9]]; inserting [4,8] into [[1,2],[3,5],[6,7],[8,10],[12,16]] returns [[1,2],[3,10],[12,16]]; and inserting [5,7] into [] returns [[5,7]].
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?