Implement a trie (prefix tree)
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Anthropic's coding round, last asked Aug 2025. It tests trie, trees and data structure design.
Implement a prefix tree over strings. Besides insert and lookup it counts words by prefix and supports deleting a word. The same word can be inserted more than once. Design the classes so new operations (autocomplete, wildcard search) can be added later without rewriting the structure.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
insert(word: str) -> None— adds one occurrence of the word. -
search(word: str) -> bool— whether at least one occurrence of exactly this word is stored. -
starts_with(prefix: str) -> bool— whether any stored word starts withprefix. -
count_prefix(prefix: str) -> int— how many stored words (counting repeats) start withprefix. -
erase(word: str) -> bool— removes one occurrence;Falseif the word isn't stored. Nodes no longer used by any word are removed.
Words are arbitrary Unicode strings, including the empty string.
Example
t.insert("apple"); t.insert("app"); t.insert("app")
t.search("apple") -> True
t.search("ap") -> False
t.starts_with("ap") -> True
t.count_prefix("app") -> 3
t.erase("app") -> True
t.search("app") -> True # one "app" is left
t.count_prefix("app") -> 2
t.erase("apple") -> True
t.starts_with("appl") -> False # the "l" and "e" nodes are gone
t.erase("banana") -> False
t.count_prefix("") -> 1
t.insert("über"); t.insert("übung")
t.count_prefix("üb") -> 2
Constraints
- Up to 2 * 10^5 operations; total length of all arguments up to 2 * 10^5 characters.
- Each operation O(L) for a word or prefix of length
L.
Clarifying questions
- Are duplicates counted, and does
eraseremove one occurrence or all of them? - What alphabet: lowercase ASCII (allowing a 26-slot array) or any Unicode?
- Is the empty string a valid word?
- Will it be used from several threads?
- Which later operations are likely: autocomplete top-k, wildcard
.matching, deletion by prefix?
What a strong answer covers
- A node with a dictionary of children, a count of words passing through it and a count of words ending at it.
count_prefixis then O(L) with no subtree walk. -
erasethat checks the word exists first, then decrements counts along the path and cuts off the first child whose pass count reaches zero. - Dictionary children for Unicode versus fixed arrays for a small alphabet, and the memory each costs.
- Keeping traversal in one helper (
_find_node(prefix)) so new operations reuse it. - Complexity per operation, and tests for duplicates, erasing a word that is only a prefix, erasing a missing word, and the empty string.
Follow-ups
-
autocomplete(prefix, k): thekmost frequent completions. - Wildcard search where
.matches any one character. - Thread safety: one lock, a read-write lock, or per-node locks.
- Cut memory with a compressed (radix) trie.
Where it's asked
- Anthropic · Coding · Last asked Aug 202555 min · Live coding on a realistic, multi-part problem, a different one from the screen · No AI
What it tests
trie · trees · data structure design
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
