Word Search
Solve Word Search by recognizing the Backtracking pattern and turning the prompt into a small invariant before coding.
Frame the problem
- Implement exist with the exact signature used by the interactive runner.
- Use the visible tests to confirm the input and output shape before reading the final solution.
- Treat challenge tests as edge-case pressure: empty inputs, repeated values, boundary shapes, or impossible states.
- State the invariant before code, then dry-run one passing case and one failing-looking case.
1. Reveal example inputs and outputs
Example 1
Input:
exist([
[
"A",
"B",
"C",
"E"
],
[
"S",
"F",
"C",
"S"
],
[
"A",
"D",
"E",
"E"
]
], "ABCCED") Output:
true 2. Brute force first
What direct brute force would be correct for a tiny input? Name the exact repeated work that the target pattern removes.
3. Reveal the insight ladder
- Map the prompt to the Backtracking pattern instead of starting from syntax.
- Mark the current cell visited during the DFS path.
- A cell cannot be reused in the same path.
- Only reveal the final code after you can explain why each state update is safe.
4. Dry run before code
- word-search-found: input [[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"ABCCED"] should produce true. Hint to check your state: Mark the current cell visited during the DFS path.
- word-search-reuse: input [[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"ABCB"] should produce false. Hint to check your state: A cell cannot be reused in the same path.
5. Reveal final Python solution
def exist(board: list[list[str]], word: str) -> bool:
rows, cols = len(board), len(board[0])
def dfs(row: int, col: int, index: int) -> bool:
if index == len(word):
return True
if row < 0 or row == rows or col < 0 or col == cols or board[row][col] != word[index]:
return False
char = board[row][col]
board[row][col] = "#"
found = dfs(row + 1, col, index + 1) or dfs(row - 1, col, index + 1) or dfs(row, col + 1, index + 1) or dfs(row, col - 1, index + 1)
board[row][col] = char
return found
return any(dfs(row, col, 0) for row in range(rows) for col in range(cols)) Complexity: Derive the exact bounds from exist: count how often each input item is visited and the maximum size of the main state structure.
Interview narration
- I will first describe the invariant in plain language.
- Then I will explain what data structure carries that invariant across the traversal, loop, recursion, or DP transition.
- Finally I will walk one edge case before writing the optimized version.
Common traps
- Solving only the visible example instead of the invariant.
- Forgetting empty input, singleton input, duplicate values, or impossible-state cases.
- Revealing the solution before doing a dry run from the starter signature.
Follow-up drills
1. How do you turn this into a timed interview answer?
Start with the invariant, give the brute force in one sentence, name the optimized state, code the core loop or recursion, and run one visible test aloud before mentioning complexity.
2. How do you scale the same pattern to a larger input?
Track which state grows with the input: hash maps and sets grow with distinct values, queues grow with frontier width, recursion grows with depth, heaps grow with active candidates, and DP tables grow with state count.
3. What should you practice from blank tomorrow?
Rewrite exist without looking at the solution, then compare only the invariant and state updates before checking syntax.
Interactive runner
Write the required Python function. Your code runs locally in this browser. Hints reveal one failing case at a time.