4 views
# Trie Prefix Insert (Practice 4602): Notes Interviewers love trie prefix insert because it checks whether you can state the invariant before coding. Here is a compact rehearsal note. ## Approach Nested dicts; mark end of word. ```python class Trie: def __init__(self): self.root = {} def insert(self, word): node = self.root for c in word: node = node.setdefault(c, {}) node['#'] = True def starts_with(self, prefix): node = self.root for c in prefix: if c not in node: return False node = node[c] return True ``` ## Complexity Linear in the dominant input size for the scan; heap/tree variants add a log factor. ## Interview pitfalls State the invariant out loud, name the complexity, then code. Skipping the invariant is the usual fail mode. ## Practicing out loud Narrate the approach, complexity and one edge case in under a minute. Disclosure: I work on Linkjob AI, a real-time interview assistant. If you want live practice help, see [here](https://www.linkjob.ai/).