cs-082 — The First Bad Version
| Field | Value |
|---|---|
| id | cs-082 |
| difficulty | Foundational |
| topics | binary search, boundary |
| lecture | lecture-21 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | lectures 1-2 (problem framing, precise pseudocode) |
| objective | recognise the pattern, translate it into precise steps, and trace it correctly |
| solution | held by the instructor; discussed after the attempt |
Problem
Versions 1..n, one onward is 'bad'. Find the first bad one with minimal checks. The canonical boundary-finding binary search.
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: n = 12 versions, first bad = 8; is_bad calls counted
- Output: first bad = 8 found with 4 calls (vs 8 linear)
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
- Find FIRST bad, not 'a' bad: when test(mid) is true, answer is at mid or before -> hi = mid.
- 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: "State the monotone predicate for this case in one sentence."
Constraints and assumptions
- Difficulty: Foundational (early-semester techniques assumed: precise statements, traces, simple loops).
- Topics: binary search, boundary.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes; naming the pattern and stating the assumption is already a pass, completing the calculation is the stretch.
- 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.