cs-077 — The 4-Digit Lock
| Field | Value |
|---|---|
| id | cs-077 |
| difficulty | Beginner |
| topics | brute force, search space |
| lecture | lecture-20 |
| 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
Enumerate all 4-digit PINs satisfying 'digits sum to 12'. Count the space first, then enumerate. When brute force is the whole solution.
Your deliverable is the enumerated answer, the space count, and the pruning rule with its measured effect. 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: 4-digit PINs, digits 0-9, digits sum to 12
- Output: count first: 10^4 = 10000 candidates; favourable = 415 (455 sums possible minus the four all-identical digits)
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
- Count the space BEFORE coding - 10,000 is trivially enumerable; the lesson is the arithmetic, not the loop.
- Brute force is a legitimate algorithm when the space is honest about its size.
- Pruning is not cheating; it is arithmetic applied early.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Which constraint is cheapest to check first, and why does order matter?"
Constraints and assumptions
- Difficulty: Beginner (no prior experience assumed).
- Topics: brute force, search space.
- 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: Bound the candidate space first; then decide whether enumeration is honest.