Word Search (Trie & Backtracking)

Data Structures
Medium
Uber
65.9K views

Given a 2D board and a word, determine if the word exists in the grid. Model the problem as a graph traversal and use an efficient pruning technique (Backtracking).

Why Interviewers Ask This

Uber interviewers ask this to evaluate your ability to model grid problems as graph traversals and implement efficient backtracking with pruning. They specifically test if you can optimize naive solutions by avoiding redundant work, a critical skill for handling large-scale location data or routing algorithms where performance directly impacts user experience.

How to Answer This Question

1. Clarify constraints: Ask about board dimensions and character sets to determine if brute force is viable. 2. Define the graph: Explain that each cell is a node connected to its four neighbors (up, down, left, right). 3. Propose the algorithm: Outline a recursive backtracking approach where you start from every cell matching the first letter. 4. Detail pruning: Emphasize marking visited cells immediately to prevent cycles and unmarking them upon return (backtracking) to allow reuse in other paths. 5. Optimize further: Suggest using a Trie if searching for multiple words, but note that for a single word, simple boolean tracking suffices. 6. Analyze complexity: Discuss time complexity O(N*M*3^L) where L is word length, explaining why pruning keeps it manageable compared to exponential growth.

Key Points to Cover

  • Modeling the grid as a graph with directional edges
  • Implementing immediate pruning when characters mismatch
  • Correctly managing the visited state via backtracking
  • Analyzing time complexity relative to word length
  • Handling edge cases like empty boards or single characters

Sample Answer

To solve the Word Search problem efficiently, I would treat the 2D board as an implicit graph where each cell represents a node connected to its adjacent neighbors. My approach starts by iterating through every cell in the grid to find potential starting points that match the first character of the target word. Once a match is found, I initiate a depth-first search using recursion. During this traversal, I maintain a visited set to mark the current path, ensuring we do not revisit cells within the same word construction, which prevents infinite loops. Crucially, before exploring neighbors, I check if the current cell matches the expected character at the current index; if not, I prune that branch immediately. This pruning technique is vital because it avoids unnecessary computation on dead-end paths, significantly reducing the search space. After exploring all valid directions from a cell, I must backtrack by unmarking the cell as visited so it can be part of a different word path starting elsewhere. This ensures the solution remains correct while optimizing for speed. For example, if the board is [['A','B'],['C','D']] and the word is 'AB', we start at (0,0), move to (0,1), find the match, and return true. If the word was 'ABC', we would explore 'B', fail to find 'C' in neighbors, backtrack, and stop. This method balances clarity with performance, aligning well with Uber's focus on scalable, efficient algorithms for real-world logistics.

Common Mistakes to Avoid

  • Failing to unmark visited cells after recursion returns, causing false negatives
  • Not checking boundary conditions before accessing array indices
  • Starting the search only from the top-left corner instead of all cells
  • Using a global visited set without resetting it between different starting positions

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