The update that evicts too much
Growing a cached value double-counts its old weight and evicts useful entries unnecessarily.
What’s going wrong
A weighted LRU cache stays under its byte budget but sometimes throws away unrelated live data when replacing an existing value. Preserve the transaction and ownership rules from the previous lessons while tracing resource accounting.
Input JSON contains limit (integer 0..1000) and at most 200 events. ["put",key,value,size] supplies a JSON scalar value and a nonnegative integer weight. If size>limit, record too_large and change nothing. Otherwise reserve only the additional capacity needed for replacement, evicting least-recent entries until the final state fits. The key being replaced may itself be the least-recent victim; after each eviction the reservation must reflect whether that old value still exists. Then insert the replacement as most-recent. Zero-weight entries are valid even at limit zero.
["get",key] records {"found":true,"value":...} and moves the key to most-recent, or records {"found":false} without mutation. Print used (sum of resident weights), writes (stored/too_large per put), reads, and entries as [key,value,size] in least-to-most-recent order. Avoid unnecessary evictions: when replacement fits using its old resident weight, no entry may be evicted. All keys are short strings. The supplied weights are authoritative; do not infer size from payload length.
Expected behavior
Maintain a resource invariant as membership and replacement ownership change.
Reproduction
{"limit":6,"events":[["put","a","A",3],["put","b","B",2],["put","b","new",3]]}{"used":6,"writes":["stored","stored","stored"],"reads":[],"entries":[["a","A",3],["b","new",3]]}{"limit":3,"events":[["put","a","A",2],["put","b","B",2]]}{"used":2,"writes":["stored","stored"],"reads":[],"entries":[["b","B",2]]}{"limit":3,"events":[["put","a",0,3]]}{"used":3,"writes":["stored"],"reads":[],"entries":[["a",0,3]]}Before you begin
- Python lists and dictionaries
- Stage failure-prone validation and preserve the complete state transition.
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.