Skip to content
Preptima
mediumCodingMidSenior

You are given a grid of letters and a word. When do you stop searching, and what stops you revisiting the same cell?

You stop on four conditions and the success check has to come first, before bounds or character comparison. Revisiting is prevented per path rather than globally: a cell is marked when the search enters it and unmarked when the search leaves it, because a cell that ruins one candidate path has to remain available to a different one.

6 min readUpdated 2026-08-15

Answer

Short answer

Solve word search with depth-first backtracking from each matching starting cell. Stop when the word index reaches the word length, reject out-of-bounds or mismatched cells, and mark a cell only for the current path. Restore the mark on exit so another candidate path can reuse that cell.

Was this answer helpful?

Likely follow-ups

  • Now match a thousand words against the same grid. What replaces one call per word, and what does it save?
  • Diagonal moves are allowed as well. Which part of your complexity argument changes?
  • The grid is a hundred thousand by a hundred thousand and held on disk. What is the first thing you change?
  • Prove to me that marking in place and restoring on exit leaves the grid identical to how you received it.

Related questions

Related Topics