cs-071 — Closest Pair, Small n
| Field | Value |
|---|---|
| id | cs-071 |
| difficulty | Intermediate |
| topics | sorting, pair scan |
| lecture | lecture-18 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | loops, lists, and tracing from lectures 1-6 |
| objective | choose the technique, trace it faithfully, and defend the boundary behaviour |
| solution | held by the instructor; discussed after the attempt |
Problem
Find the two closest points on a line. Sort once, scan adjacent pairs. Then discuss: what breaks in two dimensions? (Motivation, not implementation.)
Your deliverable is the answer for the worked instance, the precondition stated, and the not-found behaviour demonstrated. 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: points [1.5, 3.1, 9.2, 9.9, 4.0] -> closest pair (9.2, 9.9) distance 0.7
- Output: sort by value, then scan adjacent pairs only; (3.1, 4.0) is 0.9 apart — checking every pair would also find it, but slower
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
- After sorting, the closest pair must be adjacent - that is the theorem to state, then to use.
- Sorted data is not just faster — it enables different algorithms.
- A scan is only as good as its stated precondition.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Which boundary test would you keep if you could keep only one?"
Constraints and assumptions
- Difficulty: Intermediate (loops and lists assumed).
- Topics: sorting, pair scan.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes for the plan and trace; finishing the details is a bonus, defending the method is the target.
- 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: Add a stable tie-break rule and show a case where it matters.