cs-081 — Guess the Number, Formally
| Field | Value |
|---|---|
| id | cs-081 |
| difficulty | Beginner |
| topics | binary search, halving |
| lecture | lecture-21 |
| 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
The guessing game as binary search: guarantee the number of questions. Halving as an idea before any code.
Your deliverable is the located answer, the predicate, and the bound updates shown for one full trace. 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: secret in 1..100; each guess answered 'high/low/correct'
- Output: 7 guesses always suffice: ceil(log2 101); 6 for most, 7 worst case
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
- Halving argument with a drawing: after k questions, remaining <= 100 / 2^k; force it below 1.
- Binary search is a loop plus a monotone question — nothing more.
- Half-open intervals keep the arithmetic honest.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "What would you do if the predicate were monotone 'almost everywhere' with one glitch?"
Constraints and assumptions
- Difficulty: Beginner (no prior experience assumed).
- Topics: binary search, halving.
- 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: Re-solve the same instance on a monotone predicate instead of an array.