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
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.
When to use and when not to
| Situation | Trie | Hash map |
|---|---|---|
| Whole-key lookup only | O(k), more memory | O(k) to hash, then one probe, smaller |
| Prefix listing or autocomplete | O(output × k) | Full scan, O(n) |
| Longest matching prefix (routing) | Deepest flagged node on one walk | Not possible without a scan |
| Many patterns over one text | Aho-Corasick, one pass | One scan per pattern |
| Substring search of a long text | Suffix trie is quadratic; use a suffix array | Not applicable |
| Static, large dictionary | Compressed or double-array form | Hash set may still be smaller |
Traps interviewers probe
- 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'.
- 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.
- 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.
- 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.
- 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:
- What it is, and that the query is a prefix or ordering question.
- O(k) per operation, independent of key count, and why hashing is also O(k).
- The end-of-key flag, and the prefix-pair test that catches its absence.
- Memory: the child representation, compression, and the measured per-node cost.
- 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.