Paged KV Cache Allocator With Prefix Sharing

Last updated: November 29, 2026

Quick Overview

Implement a block allocator for a paged KV cache where sequences hold lists of fixed-size blocks, identical full prompt prefixes share blocks via reference counts, and writes to a shared partial block trigger copy-on-write. Support add, append token, fork and free.

Fireworks AI
Coding & Algorithms
Machine Learning Engineer
Fireworks AI
November 29, 2026
Machine Learning Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Implement a block allocator for a paged KV cache where sequences hold lists of fixed-size blocks, identical full prompt prefixes share blocks via reference counts, and writes to a shared partial block trigger copy-on-write. Support add, append token, fork and free.

Prompt caching and paged attention let an inference platform such as Fireworks reuse KV for repeated system prompts and parallel samples, which directly lowers cost per token. The interviewer wants correct reference counting, a prefix key that includes the whole history, and rollback when memory runs out.

What the Interviewer Expects
  • Key a block by its parent key plus its tokens so equal chunks at different positions do not collide
  • Share only full blocks and register a block once it fills
  • Implement copy-on-write for partial blocks after fork
  • Return blocks to the free list and drop index entries when refcount hits zero
  • Roll back partially allocated sequences on out-of-memory
Key Topics to Cover
Paged attention
Prefix caching
Reference counting
Copy-on-write
Memory allocators
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you keep freed but still indexed blocks around as an evictable cache rather than discarding them?
  • Why hash the parent key instead of the whole prefix, and what does a hash collision cost you?
  • How does beam search or n-way sampling benefit from fork?
  • What changes when blocks live on multiple GPUs under tensor parallelism?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

add_sequence(sid, prompt) splits the prompt into blocks of size B, reusing an existing block when the same full block exists after the same prefix. append_token adds one generated token, allocatin...

High-Level Design

Each full block has key (parent_key, tokens), a chained identity of the whole prefix, so a chunk only matches when everything before it matches too. by_key maps keys to blocks for lookup; key_of...


Submit Your Answer
Markdown supported

Related Questions