Manage paged KV blocks with prefix sharing
Simulate a paged cache with fork, append and release.
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
{"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}}{"block_size":2,"actions":[]}{"tables":{},"pages":{},"refcounts":{}}{"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.
Read the report, then sign in to start your investigation.