~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Trie
Prefix tree; longest-match tokenizing.
Notes
Recognise it when: prefix queries, autocomplete, longest-match tokenizing, word search over a dictionary.
root = {}
def insert(w):
node = root
for ch in w: node = node.setdefault(ch, {})
node["$"] = True
Longest match from position i: walk the trie, record the last position where "$" was present, and stop when there's no child. Emit up to that recorded position (not where the walk stopped).
Gotchas: an empty prefix matches everything. Trie vs set: a set needs O(L) substring slices per position, while a trie stops as soon as the prefix dies.
10 problems
Interview roadmap
Tries Prefix trees for words and autocomplete.
- Basics: count words by prefix with a trie basics py · c++ · java easy
- Abbreviated shell commands py · c++ · java easy
- Implement Trie (Prefix Tree) py · c++ · java easy
- Longest-match tokenizer 3 levels Anthropic medium
- Tag known phrases in a guest review Airbnb py · c++ · java medium
- OA: Radix cache for integer sequences 2 levels py · c++ · java hard
- Substring Wrapper MetaUber py · c++ · java easy
- Design Add and Search Words Data Structure py · c++ · java medium
- Word Search II py · c++ · java hard
- OA: Search box suggestions 3 levels py · c++ · java medium