bugger.
All challenges/Pythonhardthe-write-that-half-happened-v1
THE INVESTIGATION

The write that half happened

A rejected timed-cache write still evicts or corrupts entries that were already stored.

CollectionsStateRegression testing

What’s going wrong

A failed insertion reports overflow, but the cache has already changed. Use the previous ownership lesson to identify the full transaction boundary across entries, expiry cleanup and eviction.

Input JSON has capacity (1..8) and at most 200 events. Entry keys are short strings. Time starts at zero. ["advance", delta] increases time by a nonnegative integer. ["put", key, value, ttl] has integer ttl>=0; its deadline is now+ttl. The simulated clock can represent deadlines only through 100 inclusive. A larger deadline rejects the write as overflow and must leave ALL cache entries and their ordering unchanged, including expired residents. This finite ceiling replaces real datetime overflow.

For a successful put, first remove all residents with deadline<=now, remove any existing value of this key, then evict oldest-inserted entries until one slot is free. Append the new value/deadline at the newest end. ["get",key] removes expired residents, then records {"found":true,"value":...} or {"found":false}. Values are JSON scalars, so read logs need no deep copy. Advancing time alone does not remove residents.

Print now, writes (stored/overflow for each put), reads (get observations), and entries as [key,value,deadline] ordered oldest to newest. A zero TTL may remain resident until the next successful put or get, but is never readable. Errors must not leave half-initialized entries or silently evict prior data.

Expected behavior

Identify a transaction boundary and preserve every owned invariant on failure.

Reproduction

01 Rejected write must not evict a live entry
{"capacity":1,"events":[["put","a",1,10],["put","b",2,101]]}
{"now":0,"writes":["stored","overflow"],"reads":[],"entries":[["a",1,10]]}
02 Ordinary write and read
{"capacity":1,"events":[["put","a",0,10],["get","a"]]}
{"now":0,"writes":["stored"],"reads":[{"found":true,"value":0}],"entries":[["a",0,10]]}
03 Successful overflow-free replacement
{"capacity":1,"events":[["put","a",1,10],["put","a",2,20]]}
{"now":0,"writes":["stored","stored"],"reads":[],"entries":[["a",2,20]]}

Before you begin

  • Python lists and dictionaries
  • Separate owned mutable bookkeeping from observable values and history.

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.