cs-073 — The Palindrome Walk
| Field | Value |
|---|---|
| id | cs-073 |
| difficulty | Beginner |
| topics | two pointers, invariants |
| lecture | lecture-19 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | none |
| objective | state inputs, outputs, and assumptions, then solve a small instance by hand |
| solution | held by the instructor; discussed after the attempt |
Problem
Check a palindrome with two pointers from both ends. State the invariant ('everything outside the pointers is verified') and why it ends correctly.
Your deliverable is the result for the worked instance, the invariant in one sentence, and the termination argument. If the problem is ambiguous anywhere, state the assumption you are making and carry on — a stated assumption is part of a good answer.
Examples
- Input: "Never odd or even" -> palindrome; "hello" -> not
- Output: True / False via two pointers; invariant: everything outside [l, r] already verified
Solve for the worked instance first. Then ask what would change if one number, name, or condition were replaced by something stranger.
What to notice
- left and right walk inward, skipping non-alphanumerics; the loop ends when they meet or cross.
- The invariant is the algorithm; the pointers are just bookkeeping.
- If you cannot say what strictly shrinks, the loop can spin forever.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Change one movement rule and predict the first input that breaks it."
Constraints and assumptions
- Difficulty: Beginner (no prior experience assumed).
- Topics: two pointers, invariants.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes for a first attempt; partial answers with clear reasoning are worth more than silent struggling.
- Optional stretch (not required): describe an alternative approach and say when it beats the obvious one.
Hints and extension
- First nudge: identify the pattern the worked instance uses before computing anything.
- If stuck after three minutes, write the input/output line you expect and trace the smallest example by hand.
- After the reveal, attempt the extension below before the next lecture.
- Extension challenge: State the invariant in one sentence and point at the step that preserves it.