Skip to interview prep
Apexed

45. What is a trie and when would you use it?

MediumString algorithms

Technical

Type or dictate your answer. Your draft is saved on this device.

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.

Example program

What is a trie and when would you use it — Data structures & algorithms interview question — Apexed