Word Search

Algorithms
Medium
Uber
54.8K views

Given an $m \times n$ grid of characters and a string `word`, return true if `word` exists in the grid. The word can be constructed from letters of sequentially adjacent cells. Use Backtracking/DFS.

Why Interviewers Ask This

Uber interviewers ask the Word Search problem to evaluate a candidate's mastery of backtracking and depth-first search (DFS) algorithms. They specifically look for your ability to handle state management, such as tracking visited cells to prevent reusing characters in a single path. This question tests whether you can systematically explore a complex search space while managing recursion stack overflow risks and optimizing time complexity in grid-based traversal scenarios.

How to Answer This Question

1. Clarify Requirements: Confirm if the word can wrap around edges or reuse cells within the same path. Uber values candidates who ask about edge cases like empty grids or single-character words. 2. Define the Core Logic: Explain that you will iterate through every cell in the grid as a potential starting point for the word. 3. Implement DFS with Backtracking: Describe creating a recursive function that checks if the current character matches the target. Crucially, mark the cell as visited before recursing to adjacent neighbors (up, down, left, right). 4. Handle State Restoration: Emphasize the importance of unmarking the cell after the recursive call returns. This allows other paths to use the same cell, which is vital for correct backtracking. 5. Optimize and Analyze: Discuss pruning branches where characters don't match immediately and analyze the time complexity of O(N * M * 4^L), where L is the word length. Conclude by mentioning how this approach efficiently handles sparse solutions typical in Uber's real-world data problems.

Key Points to Cover

  • Explicitly mention marking cells as visited to prevent reusing the same character in a single path
  • Demonstrate understanding of backtracking by explaining the need to unmark cells after recursion
  • Clarify edge cases early, such as empty grids or words longer than the total number of cells
  • Analyze time complexity relative to the grid size and word length rather than just stating 'it works'
  • Propose checking boundaries before accessing array elements to avoid runtime errors

Sample Answer

To solve the Word Search problem efficiently, I would first validate the input grid to ensure it's not null or empty. My strategy involves iterating through every cell in the m x n matrix, treating each as a potential starting point for the target word. For each starting cell, I invoke a Depth-First Search (DFS) helper function. The DFS function takes the current coordinates and the index of the character we are trying to match. First, I check boundary conditions and verify if the current cell matches the required character. If it does, I mark the cell as visited using a temporary boolean flag or by modifying the grid temporarily to avoid cycles. Then, I recursively explore all four adjacent directions: up, down, left, and right. If any recursive call returns true, indicating the rest of the word was found, the function propagates success upward. If none of the directions yield a match, I backtrack by unmarking the cell so it remains available for other paths. This restoration step is critical; without it, valid solutions might be missed because the algorithm incorrectly assumes a cell is permanently used. Finally, if the loop completes without finding the word, I return false. In terms of complexity, this solution runs in O(rows * cols * 4^length) time in the worst case, which is acceptable for medium-difficulty constraints. This structured approach ensures robustness against edge cases while maintaining clarity, a standard expected in technical interviews at companies like Uber.

Common Mistakes to Avoid

  • Failing to unmark visited cells after the recursive call, causing the algorithm to miss valid paths due to incorrect state persistence
  • Not checking grid boundaries before accessing adjacent cells, leading to index out-of-bounds exceptions during execution
  • Starting the search from only one specific cell instead of iterating through all possible starting positions in the grid
  • Ignoring the case where the word length exceeds the total number of cells in the grid without performing an initial optimization check

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 145 Algorithms questionsBrowse all 57 Uber questions