cs-083 — Book Allocation
| Field | Value |
|---|---|
| id | cs-083 |
| difficulty | Intermediate |
| topics | binary search, search on answer |
| lecture | lecture-21 |
| 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
Split pages across k readers so the busiest reads as few as possible. Binary search the answer, feasibility check inside. Search-on-answer as a pattern.
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: pages [10, 20, 30, 40], k = 2 readers
- Output: best split: {10,20,30 | 40} max load 60 - vs {10,20 | 30,40} = 70; answer 60
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
- Binary search the ANSWER: 'can k readers finish with max load L?' is monotone in L.
- 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: Intermediate (loops and lists assumed).
- Topics: binary search, search on answer.
- 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: Re-solve the same instance on a monotone predicate instead of an array.