Browse curriculum

FAANGPrep Sprint

Prefix Matching

Use Trie to efficiently find all keys matching a prefix.

Medium

Problem Understanding

This is essentially startsWith but often implies retrieving related data or completing the word. In this lesson, we use the standard startsWith logic as the core building block for Autocomplete systems.

Algorithm Strategy

  1. Start at the Trie root.
  2. Iterate through each character of the prefix.
  3. Check if the current node has a child for that character.
  4. If No: Prefix doesn't exist. Return False.
  5. If Yes: Move to that child node.
  6. If loop completes: The path exists. Return True (unlike Search, we ignore isEnd).

Interactive Visualization

PREFIX "ap" · GREEN = END OF A STORED WORD

TRIE BUILT

Checked

0

Depth

0

Returns

—

 

The trie holds "apple", "apply", "apt", "bat"

Next

startsWith("ap"): start at the root

Words

Prefix

See how startsWith confirms if a path exists without requiring the isEnd flag.