Word Search II
Problem
You are given a rectangular grid of lowercase letters and a list of distinct words. A word is present in the grid if you can trace it by starting at some cell and repeatedly stepping to an up/down/left/right neighbor, using each cell at most once within that single trace. Return every word from the list that is present, in any order.
- The grid is at most about a dozen rows and columns.
- The word list can hold tens of thousands of entries, each up to about ten letters.
- Grid and words contain only
a–z; the words are distinct.
Examples
- Grid
[["g","c","a","r"],["p","i","t","e"],["l","e","d","s"],["y","n","o","w"]], words["pit","tied","rest","glow"]→["pit","tied"]."rest"fails because thetreachable fromp-iis not adjacent to anys;"glow"fails becauseg's only neighbors arecandp, notl. - Grid
[["m","n"],["p","q"]], words["mnpq"]→[]. Tracingm-n-pwould neednandpadjacent, which they are not. - Grid
[["z"]], words["z","zz"]→["z"]. A single cell cannot be used twice.
Hints
- Running Word Search I once per word repeats the same grid exploration many times. What is shared across all the words?
- Put the words in a trie, then DFS from every cell, advancing through the trie as you move. Stop the moment the current path is not a prefix of any word.
- When a word is found, remove it from the trie (unmark the node and prune dead branches) so later DFS calls do not re-explore paths that can only yield words already found.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.