cs-080 — The Subset Riddle
| Field | Value |
|---|---|
| id | cs-080 |
| difficulty | Advanced |
| topics | brute force, subsets, analysis |
| lecture | lecture-20 |
| 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
Pick numbers from a list to hit an exact target sum. Full enumeration is 2^n; show the cost curve, solve small n, then sketch why cleverness is needed for big n.
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: [3, 7, 1, 2, 9], target 12 -> 3+9 or 1+2+9... enumerate subsets
- Output: 2^5 = 32 subsets; subset-sum found: {3, 9} and {1, 2, 9}; cost curve doubles each +1 item
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
- Enumerate subsets via bit masks 0..2^n-1; compute the cost table for n = 10, 20, 30 to feel 2^n.
- 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: "Double the input size: is this still acceptable? Show the arithmetic."
Constraints and assumptions
- Difficulty: Advanced (functions, dictionaries, and algorithmic reasoning assumed).
- Topics: brute force, subsets, analysis.
- 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: Bound the candidate space first; then decide whether enumeration is honest.