Count KV Cache Blocks With Shared Prompt Prefixes

Last updated: November 29, 2026

Quick Overview

Given a batch of token sequences and a block size B, compute how many KV cache blocks are needed when full blocks with identical prefixes are shared between requests, and partial trailing blocks are private.

Nebius
Coding & Algorithms
Machine Learning Engineer
Nebius
November 29, 2026
Machine Learning Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a batch of token sequences and a block size B, compute how many KV cache blocks are needed when full blocks with identical prefixes are shared between requests, and partial trailing blocks are private.

Inference roles at Nebius work on serving open models at high throughput, where prefix caching (as in paged attention) is a major memory saver for shared system prompts. The interviewer checks that you see this as a trie over blocks and understand why only full blocks are shareable.

What the Interviewer Expects
  • Recognizes sharing is prefix-based, not content-based anywhere in the sequence
  • Builds a trie keyed by full block contents
  • Treats partial last blocks as private
  • Gives correct complexity in tokens
  • Connects the result to real serving memory budgeting
Key Topics to Cover
tries
prefix caching
paged attention
hashing
LLM inference
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 implement eviction of cached prefix blocks under memory pressure?
  • Two requests share a prefix but use different LoRA adapters; can they share blocks?
  • How would you hash blocks to avoid storing full token tuples as keys?
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

Each request is a list of token ids. The cache stores blocks of exactly B tokens. Block i of two requests can be shared only if blocks 0..i are identical in both, because the KV values depend on...

High-Level Design

Walk each request through a trie where each edge is the tuple of one full block. Creating a new trie node means allocating a new physical block. Count one extra private block for each request whose le...


Submit Your Answer
Markdown supported

Related Questions