Paged KV Cache Block Allocator With Prefix Sharing and Copy-on-Write

Last updated: November 29, 2026

Quick Overview

Design and implement a block allocator for an LLM inference server: KV memory is split into fixed-size blocks, sequences hold block tables, full blocks with identical token prefixes are shared via refcounts, and a forked sequence copies a shared partial block on its first write.

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

0

0

0 solved


Design and implement a block allocator for an LLM inference server: KV memory is split into fixed-size blocks, sequences hold block tables, full blocks with identical token prefixes are shared via refcounts, and a forked sequence copies a shared partial block on its first write.

Serving many concurrent requests for Mistral models on La Plateforme depends on packing KV cache tightly and reusing shared prompts such as long system prompts or agent instructions. The interviewer checks refcount correctness, a prefix hash that encodes the whole history (not just the block), copy-on-write for parallel sampling, and clean behavior when memory runs out.

What the Interviewer Expects
  • Represent each sequence as a block table and track refcounts per block
  • Key the prefix cache by a chained hash of parent hash plus block tokens
  • Share only full blocks across requests; copy-on-write partial blocks after fork
  • Return blocks to the free list and evict cache entries when refcount reaches zero
  • Report how many prompt tokens were served from cache
  • Discuss out-of-memory handling: preemption, swapping, or recompute
Key Topics to Cover
Paged attention
Reference counting
Prefix caching
Copy-on-write
Memory management
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 zero-refcount cached blocks around under an LRU policy instead of freeing them immediately?
  • How do you make add_sequence atomic when memory runs out halfway?
  • How does block size trade fragmentation against kernel efficiency?
  • How would prefix hashing change for multimodal inputs or LoRA adapters?
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

Fixed pool of N blocks of B tokens. Operations: add_sequence(prompt) (reuse cached full blocks, allocate the rest), append(token) during decode, fork for parallel sampling, free_sequence. Corr...

High-Level Design
  • Free list plus refcount array.
  • Prefix cache: hash(parent_hash, tokens_of_block) -> block. Chaining makes block k's key depend on all earlier tokens, so identical text at different offsets does n...

Submit Your Answer
Markdown supported

Related Questions