Watch how a Trie powers autocomplete: traverse the prefix path then DFS-collect all matching words. O(L + K) where K = number of completions.
About this algorithm
Autocomplete takes what someone has typed so far and returns every stored word that starts with it. A trie makes this two short phases: walk down one node per typed letter to reach the prefix, then search the subtree under that node and collect every word end you pass. Words that do not start with the prefix are never looked at.
Use a trie for autocomplete when the word list is large and a lookup runs on every keystroke: search boxes, command palettes, contact pickers, code completion. Checking every word with startsWith costs time for the whole list on every key; the trie only visits words that actually match. A sorted array with binary search for the first and last match is a lighter choice when the list rarely changes. Real products also rank the matches, often by storing a count or a short best-first list in each node, since a one-letter prefix can match thousands of words. Pick the longest common prefix page when you want what the words share rather than which words match.
From the root, follow the child for each letter typed. If a letter has no child, no stored word starts with the prefix, so return an empty list.
Run a depth-first search from the node where the prefix ended, building each word letter by letter on the way down.
Each node marked as the end of a word gives one suggestion, including the prefix node itself when the prefix is a whole word.
This page explores children alphabetically, so the suggestions come out sorted.
The page loads 12 words: app, apple, apply, application, apt, ball, bat, band, ban, cat, can and cap, and starts on the prefix ap. The walk reads a, then p, and reaches the ap node in 2 steps. Searching below it finds app first, since that node is a word end, then apple, application and apply under appl, in letter order, and finally apt. That is 5 suggestions, and the 7 words starting with b or c were never visited. The other buttons give ball, ban, band and bat for ba; can, cap and cat for ca; and 4 words for app, because apt does not sit under the app node.
Each new keystroke only moves one node further down, so a search box can keep the current node and step from it as the user types instead of starting again from the root. A backspace steps back up one node.
Time: reaching the prefix takes one step per typed letter, L, and the search below visits each of the K nodes under the prefix once. Space: the suggestions hold up to K characters between them, and the search keeps one stack frame per letter of the longest match.