The Design Add and Search Words Data Structure interview question requires implementing a dictionary that supports two operations: adding a new word and searching for a word. The "twist" is that the search query can contain the wildcard character '.', which matches any single letter. This Design Add and Search Words Data Structure coding problem is a step up from basic string matching.
Companies like Meta, Amazon, and Microsoft ask this to evaluate your mastery of the Trie (Prefix Tree) interview pattern. It tests your ability to adapt a standard data structure to handle fuzzy matching using recursion. It’s a perfect test of Depth-First Search (DFS) application on a tree-like structure.
This problem is solved using a Trie with Recursive Backtracking.
isEndOfWord.Dictionary: ["bad", "dad", "mad"].
search("pad"): Starts at root. No child 'p'. Returns false.search("bad"): Follows 'b' -> 'a' -> 'd'. isEndOfWord is true. Returns true.search(".ad"): At root, sees '.'.
true.HashSet of all words. While add is fast, search with wildcards becomes , which is too slow.[26] for every node when the alphabet is sparse (though for 'a'-'z', an array is usually acceptable).Practice implementing a basic Trie from scratch before your interview. Once you're comfortable with insert and search, adding the wildcard logic is just a matter of adding a simple DFS loop for the '.' case.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Implement Magic Dictionary | Medium | Solve | |
| Design File System | Medium | Solve | |
| Implement Trie (Prefix Tree) | Medium | Solve | |
| Remove Sub-Folders from the Filesystem | Medium | Solve | |
| Implement Trie II (Prefix Tree) | Medium | Solve |