Microsoft · Question 68 · Backtracking
Word Search II
Problem
Given a board and a list of words, find all words present in the board. You may move in 4 directions and can't revisit cells.
Approach
Trie-indexed words plus board DFS that follows Trie edges only.
Time O(M×N×4^L)Space O(K×L)
How it works
- 1Index all words in a Trie.
- 2DFS the board following Trie edges only.
- 3Collect words at terminal nodes; driver sorts for stability.
Sample input / output
Input: 4 4 oaan etae ihkr iflv 4 oath pea eat rain Output: [eat, oath]
Constraints
1 <= m, n <= 12; lowercase board and words.
Solution — read it, then walk it
Time O(M×N×4^L)Space O(K×L)Loops 7Branches 3Returns 2source · Java
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/46
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits