Word Dictionary with Wildcards (Trie)

Data Structures
Hard
IBM
71K 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…

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

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