Skip to content
Tech Interview Prep
Technical interview guide

Tries

A tree specialized for prefix operations over strings: each edge is a character, each path from the root is a prefix.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v8
Editorial status
Reviewed

Scope: Language-neutral, with container comparisons cited from CPython 3.14 and java.util as of Java 21.

Overview

Curated: · Written: · Reviewed:

Tries

In one line

A trie stores each key as a path from the root, so every shared prefix is stored once and prefix questions cost a walk of the prefix length. Reach for it when queries are about prefixes, ordering, longest matches or many patterns in one text pass; use a hash map for whole-key lookups.

Key definitions

  • End-of-key flag: a boolean on a node marking that a stored key ends there. Without it, a key that is a prefix of another looks present.
  • Radix (compressed) trie: single-child chains merged into one edge labelled with a substring. Insertion may split an edge.
  • Subtree maximum: the best score in a node's subtree, so best-first search returns the top K without visiting the whole subtree.
  • Failure link: an Aho-Corasick pointer to the longest proper suffix of the current path that is also a trie prefix.
  • Path copying: an update copies the nodes on one path and swaps the root, so readers keep a consistent snapshot without locks.

Formulas and complexity

insert, search, starts_with=O(k)\text{insert, search, starts\_with} = O(k) enumerate completions=O(output×k)\text{enumerate completions} = O(\text{output} \times k) wildcard with w dots=O(k⋅Aw),A=alphabet size\text{wildcard with } w \text{ dots} = O(k \cdot A^{w}), \quad A = \text{alphabet size} array-children memory=N×(A×p+f)\text{array-children memory} = N \times (A \times p + f)

Hypothetical check on MCQ 1: 21,000,000 nodes × (26 × 8 + 1) bytes ≈ 4.39 × 10^9 bytes, almost all of it null child pointers.

How it works

  • Each edge is one symbol and each node is the prefix spelled by its path.
  • search walks the query and returns True only if it ends on a node with the end-of-key flag. starts_with walks the same path and skips the flag check.
  • A mismatch stops the walk at the first differing character, so a miss costs its matched depth, not the whole key.
  • Autocomplete descends once to the prefix node, then expands subtrees best-first using their stored maxima.
  • Aho-Corasick follows a failure link on mismatch instead of restarting, so one pass over the text finds every pattern.
c a r d t root c ca car end card end cat end

When to use and when not to

SituationTrieHash map
Whole-key lookup onlyO(k), more memoryO(k) to hash, then one probe, smaller
Prefix listing or autocompleteO(output × k)Full scan, O(n)
Longest matching prefix (routing)Deepest flagged node on one walkNot possible without a scan
Many patterns over one textAho-Corasick, one passOne scan per pattern
Substring search of a long textSuffix trie is quadratic; use a suffix arrayNot applicable
Static, large dictionaryCompressed or double-array formHash set may still be smaller

Traps interviewers probe

  1. Using 'the node has no children' as the end-of-key test. Fix: store an explicit flag and test with a prefix pair such as 'app' and 'apple'.
  2. Calling a trie O(1) against a hash map's O(1). Fix: both are O(k) because hashing reads the key. Decide on query type and memory.
  3. Fixed 26-pointer arrays on sparse nodes. Fix: use a map or a sorted sparse child list, and pick the representation from the real alphabet and fan-out.
  4. Ranking by collecting and sorting a whole subtree. Fix: store subtree maxima and update them on the insertion path. Inserting cave=90 into the example set changes the maxima at c and ca, not at the root, which already holds 100.
  5. Normalising on insert but not on lookup. Fix: one key function for both. Use casefold, not lower(): casefold turns 'Straße' into 'strasse', lower() leaves 'straße'.

Interview tips

Open with: a trie keys each string by its path, so a prefix query costs the prefix length, whatever the number of stored keys.

Expect these follow-ups: child representation and alphabet size; per-node memory and compression; deletion that stops at the first node still used by another key; concurrency with path copying; why wildcards branch; when a suffix array beats a suffix trie; how to serve a static index with a small mutable overlay.

30-second outline:

  1. What it is, and that the query is a prefix or ordering question.
  2. O(k) per operation, independent of key count, and why hashing is also O(k).
  3. The end-of-key flag, and the prefix-pair test that catches its absence.
  4. Memory: the child representation, compression, and the measured per-node cost.
  5. When a hash map wins outright: whole-key lookups with no prefix or ordering query.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.