Radix cache for integer sequences with LRU eviction
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in SpaceXAI's onsite applied coding round, last asked Jan 2026. It tests trie, radix tree and lru.
Implement RadixCache, a cache of integer sequences (for example, token IDs of model prompts) stored in a radix tree: a trie whose edges hold runs of integers instead of single integers. Sequences that share a prefix must share the nodes for that prefix, and a long unshared tail must be stored on a single edge, not one node per integer.
Implement:
-
insert(seq: list[int]) -> int: storeseq. Return how many of its leading integers were already in the cache before this call. -
match_prefix(seq: list[int]) -> int: return the length of the longest prefix ofseqthat is stored in the cache (as part of any stored sequence). -
search(seq: list[int]) -> bool: return true only ifseqitself was inserted, not merely a prefix of something inserted. -
delete(seq: list[int]) -> bool: removeseqif it was inserted. Keep the tree compressed afterward. -
evict(num_ints: int) -> int: free at leastnum_intscached integers by removing whole leaves, least recently used first (a sequence counts as used when it is inserted or matched). Return how many integers were freed.
Also keep size: the total number of integers stored on all edges.
Constraints: up to 10^5 sequences, each up to 10^4 integers. Every operation except evict should run in time linear in the length of its argument.
Example 1
c = RadixCache()
c.insert([5, 6, 7, 8]) -> 0
c.insert([5, 6, 9]) -> 2 # edge (5,6,7,8) splits into (5,6) -> (7,8), (9)
c.size -> 5
c.match_prefix([5, 6, 7, 1]) -> 3
c.search([5, 6]) -> False
Example 2
c = RadixCache()
c.insert([1, 2, 3]); c.insert([4, 5]); c.insert([1, 2, 6])
c.match_prefix([1, 2, 3]) # touch it
c.evict(1) -> 2 # leaf (4,5) is the least recently used
c.search([4, 5]) -> False
c.search([1, 2, 6]) -> True
Clarifying questions
- Does
insertof a sequence that is already stored count as a use for LRU, and does it return its full length? - Can
evictremove a leaf that is part of a sequence still in use (no reference counts here)? - After evicting a leaf, does its parent become a leaf that can be evicted next in the same call?
- Is the empty sequence valid?
- When a leaf is evicted, is the sequence ending there removed so
searchturns false?
What a strong answer covers
- Nodes with an edge label (a list or tuple of ints), a children map keyed by the first integer of each child edge, an end-of-sequence flag and a last-used time.
-
insertwalking edges, splitting an edge at the first mismatch, and adding the remaining tail as one new edge; returning the matched length; time linear in the sequence. -
match_prefixwalking until a mismatch, counting partial edges, and updating last-used times along the path. -
deleteclearing the end flag, then removing an empty leaf and merging a parent left with one child and no end flag, so the tree stays compressed. -
evictwith a min-heap of leaves by last-used time, removing leaves and pushing parents that become leaves until enough integers are freed;sizekept current on every split, add and remove. - Tests: both examples, an insert that is a prefix of an existing sequence, deleting with a merge, evicting more than the cache holds.
Follow-ups
- Reference counts so sequences in use by running requests are never evicted.
- Store the KV-cache blocks for each node, and evict in page-sized blocks.
- Make it safe for concurrent requests.
- Share the cache across GPUs or machines and route requests to where their prefix lives.
Where it's asked
- SpaceXAI · Onsite applied coding · Last asked Jan 2026Two or three rounds, in person (Palo Alto for many roles)
What it tests
trie · radix tree · lru · design
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
