Design a Word Dictionary with Wildcards

Data Structures
Hard
Uber
108.5K 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 the Trie, creating new nodes as needed, which takes linear time relative to the word length. The challenge lies in the search function with wildcards. For exact matches, we simply follow the path. However, when encountering a dot '.', we cannot determine the next character immediately. Instead, we use a recursive DFS approach. At any node where a dot appears, we iterate through all existing children keys. For each child, we recursively call the search function for the remaining substring. If any recursive call returns true, we immediately return true. If we reach the end of the query string and the current node marks a valid word end, we return true. Otherwise, we backtrack. This ensures we explore all potential paths without unnecessary computation. For example, searching for 'c.t' would check 'cat', 'cut', 'cot', etc., by branching at the second position. While worst-case time complexity increases with many wildcards, the average case remains efficient for sparse patterns. This approach aligns well with Uber's need for scalable, high-performance search features in their logistics platforms.

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

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 57 Uber questions