bugger.
All challenges/Pythonhardllm-paged-kv-cache-v1
THE INVESTIGATION

Manage paged KV blocks with prefix sharing

Simulate a paged cache with fork, append and release.

LLM ArchitectureLLM inference

What’s going wrong

Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.

block_size > 0. Each action is [open,id], [fork,parent,child], [append,id,token] or [close,id]. IDs are strings, opens/forks introduce new IDs and other actions target live IDs. Open creates an empty page table. Fork copies its parent’s page table and increments page reference counts. Append uses the last page if it has room, copying it first when shared, or allocates a fresh page if it is full or absent. Close releases references. Allocate the smallest free nonnegative page ID. Return tables (request→page IDs), pages (string page ID→token list) and refcounts (string page ID→count) for live pages only.

Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.

Expected behavior

Simulate a paged cache with fork, append and release.

Reproduction

01 A shared partial page diverges on append
{"block_size":2,"actions":[["open","a"],["append","a",1],["fork","a","b"],["append","a",2],["append","b",3]]}
{"tables":{"a":[1],"b":[0]},"pages":{"0":[1,3],"1":[1,2]},"refcounts":{"0":1,"1":1}}
02 Empty trace
{"block_size":2,"actions":[]}
{"tables":{},"pages":{},"refcounts":{}}
03 Closing the only request frees its pages
{"block_size":2,"actions":[["open","a"],["append","a",1],["close","a"]]}
{"tables":{},"pages":{},"refcounts":{}}

Before you begin

  • Python functions, lists and dictionaries

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.