Word Dictionary with Wildcards (Trie)

Data Structures
Hard
IBM
70.9K views

Design a data structure that supports adding words and searching for words that might contain the '.' wildcard character. Use a Trie, but modify the search operation to handle the wildcard (DFS).

Why Interviewers Ask This

Interviewers at IBM ask this to evaluate your mastery of Trie data structures and your ability to adapt standard algorithms for complex constraints. They specifically test your capacity to implement Depth-First Search (DFS) recursion when handling wildcards, assessing whether you can balance time complexity trade-offs between insertion and search operations in a production-ready system.

How to Answer This Question

1. Clarify Requirements: Confirm if the wildcard '.' matches exactly one character or multiple, and define input constraints like alphabet size. 2. Define Data Structure: Propose a Trie node with an array of children pointers and a boolean flag for word endings. 3. Design Insertion: Explain the standard O(L) insertion logic where L is word length, creating nodes as needed. 4. Formulate Wildcard Search: Detail how the search function changes from iterative to recursive DFS. When encountering '.', iterate through all 26 possible child branches rather than following a single path. 5. Optimize and Analyze: Discuss pruning strategies if the subtree is empty and analyze worst-case time complexity, noting it degrades to O(26^L) without pruning but remains efficient for sparse datasets typical in enterprise applications.

Key Points to Cover

  • Demonstrating clear understanding of Trie node structure and pointer management
  • Correctly implementing recursive DFS logic to handle wildcard branching
  • Articulating the difference in time complexity between exact match and wildcard search
  • Showing awareness of edge cases like empty subtrees or null pointer exceptions
  • Connecting the solution to real-world scalability needs typical of IBM's enterprise systems

Sample Answer

To solve the Word Dictionary with Wildcards problem, I would implement a modified Trie structure. First, we define a Node class containing a map or array of 26 characters and a boolean flag indicating the end of a valid word. The insert operation remains standard, traversing down the tree and creating nodes only when necessary, ensuring O(M) time complexity where M is the word length. The critical part is the search method. For exact matches, we traverse linearly. However, when we encounter a '.', we must branch out. Instead of checking a specific index, I would write a recursive helper function. This function takes the current node and the remaining query string. If the current character is a letter, we move to that specific child if it exists. If it is a '.', we loop through every non-null child node and recursively call the search function for the rest of the string. This approach effectively performs a Depth-First Search. We return true immediately if any recursive branch finds a complete match. If all branches fail, we backtrack. While the worst-case complexity increases due to branching, this solution efficiently handles the wildcard requirement. In an IBM context, where reliability is key, I would also add boundary checks to ensure we don't access null pointers during the traversal, making the implementation robust for large-scale dictionary services.

Common Mistakes to Avoid

  • Attempting to use Breadth-First Search instead of DFS, which complicates state management for partial matches
  • Failing to check for null children before recursing, leading to runtime errors on invalid paths
  • Ignoring the fact that '.' represents exactly one character, potentially confusing it with regex '.*'
  • Not discussing time complexity implications of the exponential branching factor in the worst case

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 29 IBM questions