cs-051 — The Cache Simulator
| Field | Value |
|---|---|
| id | cs-051 |
| difficulty | Intermediate |
| topics | dictionaries, state |
| lecture | lecture-13 |
| 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
Simulate a tiny key-value cache with a size limit: inserting evicts the least-recently-used. Order plus dictionary state; eviction policy reasoning.
Your deliverable is the mapping/set answers for the worked instance, the key choice justified, and the tie-break rule stated. 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: capacity 2; operations: put a, put b, get a, put c (evicts b)
- Output: after put c: {a, c}; LRU order a (recent), c; then put d evicts a
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
- LRU = dictionary + usage order. On every touch, move the key to 'most recent'; evict the least recent when full.
- A dictionary turns 'search for the item' into 'look it up' — that is the whole win.
- Sets answer yes/no questions; dictionaries answer how-many questions.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Re-derive the tie-break rule: is it a technicality or part of the specification?"
Constraints and assumptions
- Difficulty: Intermediate (loops and lists assumed).
- Topics: dictionaries, state.
- 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: Invert the lookup (value to keys) and state when collisions appear.