hardTriesMicrosoftAmazonGoogle
Puzzle Board Harvest
A word-search generator sweeps a letter grid and collects every dictionary word spelled by connected neighboring cells.
Given an m×n board of characters and a list of words, return all words on the board. A word can be formed by sequentially adjacent cells (no reuse within the same word).
Choose approach
o
a
a
n
e
t
a
e
i
h
k
r
i
f
l
v
1/13
Narrationstep 1 of 13
Build trie from 4 words; sweep board with DFS pruning via trie.
Variables
rows=4
cols=4
words=oath, pea, eat, rain
How to think about it
- A separate DFS per word is wasteful — the trie fuses prefixes.
- Prune the moment the current letter has no trie branch.
- Clear `$word` sentinel on collection to avoid duplicates.
Edge cases to catch in interview
- No words -> empty output.
- Word longer than any grid path -> never collected.
Examples
Example 1
[]{"grid":[["a","b"],["c","d"]],"words":["abcb"]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed