Find the Longest Cached Prompt Prefix With a Token Trie

Last updated: November 29, 2026

Quick Overview

Build an index over cached prompt prefixes (token id sequences) that supports insert, remove and a query returning the length of the longest cached entry that is a prefix of a new request.

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

0

0

0 solved


Build an index over cached prompt prefixes (token id sequences) that supports insert, remove and a query returning the length of the longest cached entry that is a prefix of a new request.

Prompt caching on the Claude API lets a request reuse computed attention state for a previously cached prefix, which only pays off if lookup is fast and exact. This question appears as an onsite coding round for inference and API teams, where the interviewer checks trie fluency, the difference between passing through a node and ending at one, and correct deletion that prunes dead branches.

What the Interviewer Expects
  • Chooses a trie keyed by token id and explains why hashing every prefix is wasteful
  • Distinguishes a cached entry ending at a node from merely passing through it
  • Returns the longest full cached entry, not the longest shared path
  • Implements removal that handles duplicate inserts and prunes nodes no entry uses
  • States complexity in terms of query length rather than number of cached entries
Key Topics to Cover
tries
prefix matching
reference counting
prompt caching
radix trees
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
  • Real systems cache in fixed-size blocks of tokens. How does the structure change if matches only count at block boundaries?
  • How would you add LRU eviction across entries while keeping lookup cost proportional to query length?
  • The trie gets millions of entries sharing a long system prompt. How would a radix (compressed) trie help?
  • How would you shard this index across inference hosts so a request lands where its prefix is cached?
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

Entries are token id lists. insert(tokens) adds an entry (duplicates allowed, so I count them). longest_cached_prefix(tokens) returns the largest L such that tokens[:L] equals some entry, or 0...

High-Level Design

A trie where each edge is a token. Each node stores terminal (entries ending here) and passing (entries whose path covers this node). Query walks down while edges exist and records the deepest nod...


Submit Your Answer
Markdown supported