45. What is a trie and when would you use it?
MediumString algorithms
Technical
Show model answer
A trie stores strings character by character along paths from the root, so words with a shared prefix share nodes. Insert and lookup take O(L) for a string of length L, independent of the number of words. Uses: autocomplete, spell checking, prefix counting and IP routing. Ternary search trees reduce memory; suffix trees index every suffix for fast substring queries.