Trie
Store words by shared prefixes to support efficient prefix search and dictionary lookup.
The task repeatedly checks prefixes, autocomplete suggestions, or a dictionary of words.
How to approach it
Walk or create one child per character. Mark word endings separately from mere prefix nodes.
A trie can use substantial memory, especially with a wide child array; consider maps or compressed representations.
Practice question
After inserting 'cat' and 'car', does the trie contain the prefix 'ca'? Is 'ca' itself a complete word?
Words = ['cat', 'car']Give your answer before revealing the explanation.Show a hint
A prefix node and an end-of-word marker represent different facts.
Show an answer
Prefix 'ca' exists; 'ca' is not a complete word unless it was inserted too.
Trie at work
A phone keyboard has saved contacts 'Maya', 'Mayank', and 'Ravi'. Which suggestions should appear after the user types 'May'?
Show one possible approach
Maya and Mayank. Walk the trie through M-a-y, then list stored words reachable below that prefix node.
Practice next
Use these problem IDs to find additional practice for this pattern: