← Questions

Radix cache for integer sequences with LRU eviction

0
SpaceXAICodingLast asked Jan 2026

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: store seq. 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 of seq that is stored in the cache (as part of any stored sequence).
  • search(seq: list[int]) -> bool: return true only if seq itself was inserted, not merely a prefix of something inserted.
  • delete(seq: list[int]) -> bool: remove seq if it was inserted. Keep the tree compressed afterward.
  • evict(num_ints: int) -> int: free at least num_ints cached 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 insert of a sequence that is already stored count as a use for LRU, and does it return its full length?
  • Can evict remove 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 search turns 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.
  • insert walking 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_prefix walking until a mismatch, counting partial edges, and updating last-used times along the path.
  • delete clearing 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.
  • evict with a min-heap of leaves by last-used time, removing leaves and pushing parents that become leaves until enough integers are freed; size kept 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

What it tests

trie · radix tree · lru · design

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding