Week 06 — Two Pointers + Sliding Window: Linear-Time Mastery
Month 02 · Week 06 of 24 · 5 flagship problems · Pattern focus: Two Pointers and Sliding Window
Theme: The two patterns that turn quadratic into linear.
Why this week matters
Two pointers and sliding window are the highest-ROI patterns in the interview — they appear in 30–40% of FAANG onsites by themselves or as building blocks. They share the same physical insight: instead of nested loops over (i, j) pairs, advance two indices in lockstep, each at most N times → O(N) total work.
What separates Mid from Senior+ on these problems is recognizing them. The trap is that they look like array problems, so candidates write nested loops and ship O(N²). The signal interviewers look for: do you reach for two pointers / sliding window first?
Specifically:
- Two pointers comes in three shapes — converging (3Sum, Container w/ Most Water), same-direction (slow/fast on linked lists, partition), and meeting (palindrome check).
- Sliding window comes in two shapes — fixed-size (count distinct in window of K) and variable-size (expand-then-contract on a constraint, e.g., longest substring with property P).
- The variable-size variant is where most candidates fail. The discipline: grow the window via right pointer until invariant breaks; shrink via left until invariant restored; record answer at every valid state.
This week’s problems are picked to span the full spectrum: 3Sum (converging two-pointer after sort), Container w/ Most Water (converging two-pointer with a non-obvious move-rule proof), Trapping Rain Water (converging two-pointer + the monotonic-stack alternative — a single problem with three distinct optimal algorithms), Longest Substring Without Repeating (variable-size sliding window, the canonical), Minimum Window Substring (variable-size sliding window, Hard).
Problem set
| # | Problem | LC # | Difficulty | Pattern | Why it’s here |
|---|---|---|---|---|---|
| p26 | 3Sum | 15 | Medium | Converging 2-pointer after sort | The most-asked array problem. Tests dedup discipline, sort-then-pivot recognition, off-by-one resilience. |
| p27 | Container With Most Water | 11 | Medium | Converging 2-pointer | The pattern in its purest form. Move-rule correctness PROOF is the senior signal. |
| p28 | Trapping Rain Water | 42 | Hard | Two pointers / Prefix-suffix max / Monotonic stack | A SINGLE problem with three legit optimal solutions. The interviewer learns more from you here than from any other problem. |
| p29 | Longest Substring Without Repeating Characters | 3 | Medium | Variable sliding window | The canonical variable-window. Master this and 70% of sliding-window problems are mechanical. |
| p30 | Minimum Window Substring | 76 | Hard | Variable sliding window | The “hardest easy problem.” Window + frequency counter + match counter. The discipline test. |
Daily schedule (suggested)
| Day | Focus | Time |
|---|---|---|
| Mon | p26 3Sum — read, attempt, code, stress | 90 min |
| Tue | p27 Container w/ Most Water — read, attempt, prove the move rule | 60 min |
| Wed | p28 Trapping Rain Water — attempt; then read all 3 optimal approaches | 120 min |
| Thu | p29 Longest Substring Without Repeating Chars — full FRAMEWORK pass | 75 min |
| Fri | p30 Minimum Window Substring — attempt; expect to need hints; that’s fine | 120 min |
| Sat | Re-attempt p28 and p30 from scratch in <30 min each. This is the readiness gate. | 90 min |
| Sun | Review — write 1-paragraph postmortem per problem in your own words. Anki cards. | 45 min |
Readiness gate (before Week 7)
You should be able to:
-
Write 3Sum in under 15 minutes with correct dedup (no
setof tuples cheat). - Prove (verbally) why Container With Most Water’s “move shorter pointer” rule is correct.
- Sketch ALL THREE Trapping Rain Water solutions — two-pointer, prefix-max, monotonic stack — and say one sentence on when each is preferable.
- Write Longest Substring Without Repeating in under 10 minutes from scratch using the canonical “char → last-index” map.
- Write Minimum Window Substring in under 25 minutes with the match counter optimization (not naive map equality on every shrink).
- State the two-pointer correctness invariant: “the answer cannot use any pair we’ve eliminated.”
- State the variable sliding-window template: expand right until invariant breaks → shrink left until invariant restored → record answer at every valid state.
If any of these are weak, do not advance. Re-do the failing problem and write the postmortem.
Anti-pattern hit-list (avoid)
- Using a hash set to dedup 3Sum triplets — works, but loses the “sorted-array dedup by skip” signal. Senior+ shows the skip-dedup.
- Moving both pointers simultaneously in Container w/ Most Water — drops the move-rule proof.
- Solving Trapping Rain Water with only ONE approach. The follow-up is always “what’s another way?”
- Variable sliding window where you forget to record the answer inside the while-loop (off-by-one trap).
- Minimum Window Substring with
map_a == map_bcheck on every shrink — that’s O(σ) per shrink → O(Nσ) total. Use a match counter that increments/decrements on each char.
Connections forward
- Week 7 — Prefix sums and binary search (extends the “scan once” mindset to range queries and search-on-monotone).
- Week 8 — Monotonic stacks and intervals (p28 Trapping Rain Water is the bridge problem).
- Phase 02 Lab — lab-01-two-pointers.md, lab-02-sliding-window.md.