Prompt Prefix Cache With Token Trie and LRU Eviction
Last updated: November 29, 2026
Quick Overview
Design a cache of token sequences for an inference server that returns the longest cached prefix of a new prompt and, when the total cached token count exceeds capacity, evicts least recently used leaf tokens first so shared prefixes survive.
Truefoundry
November 29, 20260
0
0 solved
Design a cache of token sequences for an inference server that returns the longest cached prefix of a new prompt and, when the total cached token count exceeds capacity, evicts least recently used leaf tokens first so shared prefixes survive.
Teams deploying LLMs on TrueFoundry care about GPU efficiency, and reusing KV cache for shared system prompts is one of the biggest wins in serving. The interviewer wants a radix-style trie, an eviction policy that never removes an interior node another branch depends on, and an efficient way to find the LRU leaf.
What the Interviewer Expects
- Trie keyed by token id with longest-prefix match
- Capacity measured in cached tokens
- Evict only leaves, LRU first, cascading upward when a parent becomes a leaf
- Efficient eviction via a lazy heap rather than a full scan
- Never evict the path just inserted
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How does this map onto paged KV blocks instead of single tokens?
- How would you compress chains of single-child nodes (radix tree) and what changes?
- How do you route requests across replicas to maximize prefix hits?
- What should happen to entries pinned by in-flight requests?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
match(tokens) returns how many leading tokens are cached and marks them as used. insert(tokens) adds the sequence. Size is the number of trie nodes (one per cached token position). After an insert...
High-Level Design
Each node stores its token, parent, children dict and a last access tick. Touching a path sets every node on it to the same new tick, so an ancestor is always at least as recent as its most recent d...