DSA With AI
Week 9 ยท Intervals

Non-overlapping Intervals

Solve Non-overlapping Intervals by recognizing the Intervals pattern and turning the prompt into a small invariant before coding.

Medium Intervals Week 9 Practice runner

Frame the problem

  • Implement erase_overlap_intervals with the exact signature used by the interactive runner.
  • Use the visible tests to confirm the input and output shape before reading the final solution.
  • Treat challenge tests as edge-case pressure: empty inputs, repeated values, boundary shapes, or impossible states.
  • State the invariant before code, then dry-run one passing case and one failing-looking case.
1. Reveal example inputs and outputs

Example 1

Input:

erase_overlap_intervals([
  [
    1,
    2
  ],
  [
    2,
    3
  ],
  [
    3,
    4
  ],
  [
    1,
    3
  ]
])

Output:

1
2. Brute force first

What direct brute force would be correct for a tiny input? Name the exact repeated work that the target pattern removes.

3. Reveal the insight ladder
  1. Map the prompt to the Intervals pattern instead of starting from syntax.
  2. Keep the interval with the earliest end when conflicts occur.
  3. Only one of identical overlapping intervals can remain.
  4. Only reveal the final code after you can explain why each state update is safe.
4. Dry run before code
  1. erase-one: input [[[1,2],[2,3],[3,4],[1,3]]] should produce 1. Hint to check your state: Keep the interval with the earliest end when conflicts occur.
  2. erase-two: input [[[1,2],[1,2],[1,2]]] should produce 2. Hint to check your state: Only one of identical overlapping intervals can remain.
5. Reveal final Python solution
def erase_overlap_intervals(intervals: list[list[int]]) -> int:
    intervals = sorted(intervals, key=lambda interval: interval[1])
    removed = 0
    prev_end = float("-inf")
    for start, end in intervals:
        if start < prev_end:
            removed += 1
        else:
            prev_end = end
    return removed

Complexity: Derive the exact bounds from erase_overlap_intervals: count how often each input item is visited and the maximum size of the main state structure.

Interview narration

  • I will first describe the invariant in plain language.
  • Then I will explain what data structure carries that invariant across the traversal, loop, recursion, or DP transition.
  • Finally I will walk one edge case before writing the optimized version.

Common traps

  • Solving only the visible example instead of the invariant.
  • Forgetting empty input, singleton input, duplicate values, or impossible-state cases.
  • Revealing the solution before doing a dry run from the starter signature.

Follow-up drills

1. How do you turn this into a timed interview answer?

Start with the invariant, give the brute force in one sentence, name the optimized state, code the core loop or recursion, and run one visible test aloud before mentioning complexity.

2. How do you scale the same pattern to a larger input?

Track which state grows with the input: hash maps and sets grow with distinct values, queues grow with frontier width, recursion grows with depth, heaps grow with active candidates, and DP tables grow with state count.

3. What should you practice from blank tomorrow?

Rewrite erase_overlap_intervals without looking at the solution, then compare only the invariant and state updates before checking syntax.

Interactive runner

Write the required Python function. Your code runs locally in this browser. Hints reveal one failing case at a time.