Block-Level Prefix Cache with LRU Eviction for Shared Prompts

Last updated: November 29, 2026

Quick Overview

Build a prefix cache over token sequences where each tree node owns one KV block: a new request reuses the longest cached prefix, inserts its remaining full blocks, and unreferenced leaves are evicted least recently used first when capacity runs out.

Together AI
Coding & Algorithms
Software Engineer
Together AI
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Build a prefix cache over token sequences where each tree node owns one KV block: a new request reuses the longest cached prefix, inserts its remaining full blocks, and unreferenced leaves are evicted least recently used first when capacity runs out.

Many Together AI customers send the same long system prompt or few-shot preamble on every call, and agent workloads resend growing conversation histories, so reusing cached KV for shared prefixes cuts prefill cost and time to first token. This hard onsite problem checks a tree keyed by token blocks, pinning of in-use nodes, and correct leaf-only LRU eviction.

What the Interviewer Expects
  • Keys the tree by full token blocks so a node maps to one physical KV block
  • Pins nodes on the matched and inserted path with reference counts
  • Evicts only unreferenced leaves, so a parent becomes a candidate after its last child goes
  • Uses a heap with lazy deletion or another efficient LRU structure
  • Degrades gracefully when every block is pinned
Key Topics to Cover
prefix caching
radix tree
LRU eviction
reference counting
KV cache reuse
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 route requests across many replicas to maximize prefix cache hits without overloading one replica?
  • Why cache only full blocks, and what do you lose on the trailing partial block?
  • How does a compressed radix tree change memory use and lookup cost?
  • How would you handle a tokenizer change or model version change for cached entries?
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

Capacity is a number of KV blocks of block_size tokens. acquire(tokens, now) returns how many leading tokens were already cached (so prefill can skip them) and a path of nodes the request now pins...

High-Level Design

A trie whose edges are tuples of block_size token ids. Each node stores ref (running requests using it) and last (last access time). Matching walks down while the next block exists. Inserting ad...


Submit Your Answer
Markdown supported

Related Questions