Design a Word Dictionary with Wildcards

Data Structures
Hard
Uber
108.7K views

Design a data structure that supports adding words and searching for words. The search query can contain the dot '.' wildcard character, which matches any one letter. Use a Trie and Backtracking/DFS.

Why Interviewers Ask This

Uber asks this to evaluate a candidate's ability to balance time complexity with space efficiency in real-world routing systems. They specifically test your grasp of Trie structures for prefix optimization and your skill in implementing backtracking algorithms to handle pattern matching. This problem simulates scenarios where search queries are fuzzy or incomplete, requiring robust data structure design under strict performance constraints.

How to Answer This Question

1. Clarify Requirements: Confirm if the dictionary is case-sensitive, supports only lowercase letters, and define the maximum word length. Ask about memory constraints typical at Uber. 2. Propose Data Structure: Suggest a Trie (Prefix Tree) where each node represents a character. Explain that 'addWord' is O(m) while 'search' varies based on wildcards. 3. Define Search Logic: Distinguish between standard search (O(m)) and wildcard search. For '.', explain that you must recursively visit all children nodes of the current level, not just one path. 4. Implement Backtracking: Describe a Depth-First Search (DFS) function that takes the current node and word index. If the character is '.', iterate through all non-null children; if it's a letter, move to the specific child if it exists. 5. Optimize and Edge Cases: Discuss pruning branches early if no match is possible. Handle edge cases like empty strings or patterns longer than stored words. 6. Complexity Analysis: Conclude by stating Time Complexity is O(N * 26^k) where k is wildcard count, and Space is O(Total Characters).

Key Points to Cover

  • Demonstrating clear understanding of Trie node structure and insertion logic
  • Correctly implementing recursive backtracking to handle '.' wildcard branching
  • Explicitly analyzing time complexity trade-offs between storage and search speed
  • Proactively discussing edge cases like empty inputs or full wildcard patterns
  • Connecting the solution to real-world scalability needs similar to Uber's systems

Sample Answer

To solve this efficiently, I propose using a Trie data structure. First, we define a Node class containing a map of children characters and a boolean flag indicating if a word ends there. When adding a word, we traverse…

Common Mistakes to Avoid

  • Using a hash set for storage which fails to optimize for prefix-based lookups and wildcard traversal
  • Attempting iterative solutions for the wildcard search without managing stack state correctly
  • Forgetting to check the 'isEndOfWord' flag when the search pattern fully matches a path
  • Overlooking the exponential time complexity growth when multiple consecutive wildcards exist

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 166 Data Structures questionsBrowse all 57 Uber questions