Implement Trie (Prefix Tree)
Design a trie with insert(word), search(word), and startsWith(prefix).
Intuition
A trie stores words character by character down a tree: each node has up to 26 children, and words sharing a prefix share a path. Insert walks the word, creating missing children; search walks and checks an end-of-word flag; startsWith is the same walk without the flag. Every operation costs the word's length — independent of how many words are stored.
Approach
Node = children map + flag
Each node: dict char→child, plus end marking that some word terminates here. The flag is what separates search('app') from startsWith('app') when only 'apple' was inserted.
Insert = walk and create
From the root, follow each character's child, creating nodes as needed; set end on the last.
Search vs prefix
Both walk the same way and fail on a missing child. search additionally requires node.end at the last character.
Solution & live demo
Edge cases
Walk succeeds but end is False → False.
No new nodes; just flag an interior node as end.
Idempotent — walk exists, flag already set.