← All Patterns

Pattern Recognition Drill

#107

Medium Tries

The Problem

Implement a trie with insert, search, and startsWith methods.

What approach would you use?

Think about it before scrolling down.

Implement Trie (Prefix Tree)

Key Signals

Trie

Build tree of characters. Insert walks/creates nodes. Search checks end flag. startsWith just walks. O(L) per op.

Alt: Hash Map

Store all words in a set (search is O(L)), but startsWith requires scanning all words.

Common Trap

Don't forget the `end_of_word` boolean flag — search and startsWith differ only by this check.

← #106 #108 →