Design Add and Search Words Data Structure
Build a Trie data structure for storing and searching words Trie: tree where each node represents a character Example: add_word("bad"), search("bad") -> True search("pad") -> False
A trie stores the words; the dot is what makes search interesting. On a wildcard I branch into every child at once.
Examples
You write
TrieNode
WordDictionary: add_word(word: str), search(word: str)
Example 1
- Input
- WordDictionary(), addWord("bad"), search("bad")
- returns
- true
Example 2
- Input
- WordDictionary(), addWord("bad"), search("pad")
- returns
- false
The pattern behind it
Strings
I reach for it when
- The input is text and the answer is a count, a substring or a new string.
- You need to compare, reverse or rebuild characters.
Say this before you type. Say which array pattern the string is hiding before you touch a character.
All 51 Strings problemsTrees
I reach for it when
- Each node has children, and the answer depends on the subtrees.
- The statement says depth, height, ancestor or path.
Say this before you type. Say what a call returns for an empty tree, and what it returns for a node.
All 22 Trees problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Each locked part opens in the editor once the part before it passes.
Prep guides that list this problem
Our company tags come from public interview write-ups. None of these is a confirmed question.