Wildcard Word Dictionary
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace
Problem
Implement WordDictionary.addWord(word) and search(pattern). Lowercase words are stored; '.' in a search pattern matches any single letter.
Starter code
class WordDictionary:
def __init__(self):
passTest cases
wildcards
{
"operations": [
"WordDictionary",
"addWord",
"addWord",
"addWord",
"search",
"search",
"search",
"search"
],
"arguments": [
[],
[
"bad"
],
[
"dad"
],
[
"mad"
],
[
"pad"
],
[
"bad"
],
[
".ad"
],
[
"b.."
]
]
}Expected: [null,null,null,null,false,true,true,true]
Wizard outline
- Step 1: Initialize WordDictionary
Replace the empty starter with the first real state owned by WordDictionary. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Assemble the primary transition
Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.
- Step 3: Pass the Wizard Stateful Dictionary Exact case
Complete the readable core algorithm for one representative Interview case. Literal traversal establishes the same length and complete-word rules wildcard branches must preserve.
- Step 4: Harden the Wizard Stateful Dictionary Wildcards boundary
Repair the reviewed boundary and pass the complete submission contract. Recursive branching handles any wildcard position while the terminal marker still enforces exact length.
Footguns and prerequisites
- A dot matches exactly one character, not zero or an arbitrary-length suffix.
- strings
- recursion and backtracking
Reviewed references
Practice prerequisites
- Follow Trie Edges(opens in a new tab)
Follow Trie Edges isolates after matching k characters, node is the trie node reached by exactly query[:k]; the first missing edge ends the match. That focused state discipline is required when implementing word dictionary wildcard as a complete Interview Problem.
Recommended approach and implementation
Insert into a terminal-marked trie. Search recursively by index and node; a letter follows one child, while dot tries every non-terminal child.
Why it works: At each pattern position DFS explores exactly the trie edges allowed by that character. Reaching pattern end succeeds only at a terminal node, so accepted paths correspond exactly to stored words of matching length.
class WordDictionary:
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
def __init__(self):
self.root = {}
self.end = '$'
def addWord(self, word):
node = self.root
for character in word:
node = node.setdefault(character, {})
node[self.end] = True
def search(self, pattern):
def visit(index, node):
if index == len(pattern):
return self.end in node
character = pattern[index]
if character != '.':
return character in node and visit(index + 1, node[character])
return any(key != self.end and visit(index + 1, child) for key, child in node.items())
return visit(0, self.root)