← Questions

Implement a trie (prefix tree)

0
AnthropicCodingLast asked Aug 2025

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 with prefix.
  • count_prefix(prefix: str) -> int — how many stored words (counting repeats) start with prefix.
  • erase(word: str) -> bool — removes one occurrence; False if 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 erase remove 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_prefix is then O(L) with no subtree walk.
  • erase that 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): the k most 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

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