~/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.

Tries

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.

esc