bugger.
All challenges/Pythonmediumthe-copy-that-moves-the-original-v1
THE INVESTIGATION

The copy that moves the original

A copied LRU cache unexpectedly changes its source cache's entries and eviction order.

CollectionsStateRegression testing

What’s going wrong

A cache clone must have independent bookkeeping while remaining a shallow copy of its payloads. Copying only the outer object is not enough.

Input JSON contains capacity (1..8) and events (at most 200). Cache a starts empty. Cache names and entry keys are short strings. Events are ["put", cache, key, value], ["get", cache, key], ["delete", cache, key], ["copy", new_cache, source_cache], or ["append", cache, key, item]. Copy names are new; all referenced caches exist. append targets an existing list payload and deliberately mutates that shared shallow-copy value without changing LRU order. Payloads are ordinary JSON values.

put inserts/replaces a key at the most-recent end and evicts the least-recent key when adding beyond capacity. get returns {"found":true,"value":...} and moves an existing key to most-recent, or {"found":false} without mutation. delete of a missing key does nothing. Copy duplicates entries AND their order but shares payload objects.

Print {"reads":[...],"caches":{...}}. Each get observation is frozen at its event time. Each final cache view is an array of [key,value] in least-to-most-recent order. Cache names are emitted lexically. Repair structural sharing without deep-copying payloads.

Expected behavior

Draw ownership boundaries and separate structural copies from shared payloads.

Reproduction

01 Insertion into a copy stays independent
{"capacity":2,"events":[["put","a","x",1],["copy","b","a"],["put","b","y",2]]}
{"reads":[],"caches":{"a":[["x",1]],"b":[["x",1],["y",2]]}}
02 Ordinary LRU access
{"capacity":2,"events":[["put","a","x",1],["put","a","y",2],["get","a","x"]]}
{"reads":[{"found":true,"value":1}],"caches":{"a":[["y",2],["x",1]]}}
03 Copies initially contain the same entries
{"capacity":2,"events":[["put","a","x",1],["copy","b","a"]]}
{"reads":[],"caches":{"a":[["x",1]],"b":[["x",1]]}}

Before you begin

  • Python lists and dictionaries
  • Classify what is a container and which ordering semantics belong to it.

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.