cs-032 — The Flaky Function
| Field | Value |
|---|---|
| id | cs-032 |
| difficulty | Advanced |
| topics | testing, randomness |
| lecture | lecture-08 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | functions, dictionaries, and earlier algorithmic patterns |
| objective | design the algorithm, verify it on edges, and argue correctness or trade-offs |
| solution | held by the instructor; discussed after the attempt |
Problem
A function uses randomness; tests pass sometimes. Discuss asserting distributions instead of exact values, seeding, and statistical tests for randomized code.
Your deliverable is the complete test table with expected outputs, the boundary rationale, and the automated run result. 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: randint-based roller; assert exact output fails intermittently
- Output: seed for tests; assert invariants (range, distribution over 10k trials)
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
- Two tools: seed the generator for determinism; assert properties of the distribution for correctness.
- A test case is a falsifiable claim about the requirement.
- Boundaries are where specifications and implementations disagree.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Which row in your table would you defend hardest, and why?"
Constraints and assumptions
- Difficulty: Advanced (functions, dictionaries, and algorithmic reasoning assumed).
- Topics: testing, randomness.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes for a defensible plan; a complete solution may take longer and that is expected - state where you would resume.
- 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 two boundary rows to your test table and justify each.