Lecture 19 — Invariants and Two Pointers

Position in the course

This is lecture 19 of 32. It belongs to the course arc described in the course outline and uses the standard interactive case method: display, attempt (5 minutes), discuss, reveal, name the technique.

Learning objectives

By the end of this lecture students can do all three under time pressure, which is what the four cases exercise.

Prerequisite knowledge

Before this lecture: Lectures 10 and 18 — strings/sorting

Assumed fluencies:

Preparation task: Redo cs-073's palindrome walk with pen and two fingers.

Detailed topic outline

Core (mandatory):

  1. invariants: the property that never breaks
  2. two pointers from both ends
  3. pair-sum contracts on sorted data
  4. partitioning in one pass

Extension (optional enrichment):

  1. slow/fast pointers and cycle detection
  2. proving correctness by invariant

Case sequence

CaseTitleDifficulty
cs-073The Palindrome WalkBeginner
cs-074The Pair Sum ContractFoundational
cs-075Partition by ParityIntermediate
cs-076The Slow-Fast CycleAdvanced

Display each case full-screen from the student page (cases cs-073–cs-076); instructor pages cs-073–cs-076 under case-studies/instructor/ hold the full solutions, reveal guides, and discussion prompts.

Timing plan (120 minutes)

MinutesSegment
0–10Framing: where this fits; recall of the previous lecture
10–30Case 1 (cs-073): 5-minute attempt, then discussion and reveal
30–50Case 2 (cs-074): same rhythm
50–70Case 3 (cs-075): same rhythm, harder case
70–75Break
75–100Case 4 (cs-076): same rhythm, hardest case
100–115Synthesis: name the techniques; misconceptions check
115–120Exit questions, homework, next-lecture preview

Timing flexes with the room: if a case ignites the class, let the discussion run and compress the synthesis; if the attempts stall, harvest partial attempts rather than extending the clock.

Worked examples

Instructor demonstration to open the lecture: Palindrome walk (cs-073) with two students as pointers walking inward; every disagreement is announced aloud.

Classroom activities

#ActivityMinutesFormat and prompt
1Invariant announcement6Pairs state cs-074's invariant aloud before any code; the class falsifies weak versions.
2Two-pointer dance7Nine students, one sum target, pointers move physically; each move justified.
3Tortoise-and-hare trace6cs-076 on a 6-node drawn ring; the meeting point is predicted then traced.

Board plan

Instructor explanation notes

Reading the room. Invariants are introduced as promises, not proofs: state the promise, keep the promise, and correctness follows. The physical two-pointer demos carry the lecture.

Running the reveal. cs-076 (slow/fast) is Expert; the accessible core is the termination argument — the gap that closes — rather than the full cycle-detection story.

Lecture focus. One sentence to repeat verbatim: State the invariant as a promise; never break the promise.

Common misconceptions

Each case's instructor page lists the specific wrong turn to expect; the reveal should address it by name.

Student practice exercises

Core:

  1. State and verify the pair-sum invariant (cs-074) on sorted data for three targets.
  2. Partition 10 numbers by parity in one pass (cs-075); annotate the invariant at each swap.
  3. Trace the slow/fast walk (cs-076) on a drawn 8-node ring until pointers meet.

Enrichment (optional):

Formative assessment

Exit questions (choose one; answer on a card in 60 seconds):

  1. State cs-074's invariant in one sentence. Expected: the answer pair, if it exists, lies within the current window
  1. Why do two pointers give linear cost? Expected: each pointer moves at most n times; total moves 2n
  1. What ends the slow/fast walk's doubt? Expected: the gap strictly shrinks — termination is provable

Cards are skimmed before the next lecture; three answers become the framing. Persistent wrong answers route students to the review stations of lecture 16 or to office hours.

Homework and preparation

Learning-outcome mapping

Differentiation