Prefix-Sharing KV Cache Block Allocator for an Inference Server

Last updated: November 29, 2026

Quick Overview

Implement a KV cache allocator that splits each request's tokens into fixed-size blocks, reuses already-computed full blocks when a new request shares the same token prefix, refcounts shared blocks, and keeps released full blocks cached for reuse until memory pressure forces LRU eviction.

Cerebras
Coding & Algorithms
Software Engineer
Cerebras
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Implement a KV cache allocator that splits each request's tokens into fixed-size blocks, reuses already-computed full blocks when a new request shares the same token prefix, refcounts shared blocks, and keeps released full blocks cached for reuse until memory pressure forces LRU eviction.

An OpenAI-compatible inference service sees many requests sharing the same system prompt, and reusing those prefix blocks cuts time to first token. Inference platform interviews use this to check data structure design, refcount correctness and eviction policy under a fixed memory budget.

What the Interviewer Expects
  • Keys a block by its full prefix (chained hash), not just its own tokens
  • Refcounts shared blocks and frees only at zero
  • Keeps zero-ref full blocks in an LRU cache instead of discarding them
  • Makes allocation all-or-nothing when memory is insufficient
  • Treats a partial tail block as private and never shares it
Key Topics to Cover
hash maps
reference counting
LRU eviction
KV cache
prefix caching
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 make the prefix key cheap (rolling hash) and handle collisions?
  • How does this change if decode appends tokens one at a time to the tail block?
  • What eviction policy beats plain LRU for multi-tenant traffic?
  • How do you handle preemption when a running request needs a block and none are free?
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

PrefixKVAllocator(num_blocks, block_size). allocate(seq_id, tokens) returns how many blocks had to be newly computed, or -1 with no state change if memory is short. release(seq_id) drops the seq...

High-Level Design

Block i's identity is the whole prefix through block i, so I build a chained key (prev_key, chunk). Python hashes tuples structurally; production code would use a rolling 64-bit hash plus a collisio...


Submit Your Answer
Markdown supported

Related Questions