Build All Word Squares From a Crossword Vocabulary
Last updated: November 29, 2026
Quick Overview
Given a set of distinct words of equal length, return every word square: a sequence of words where row k reads the same as column k.
0
0
0 solved
Given a set of distinct words of equal length, return every word square: a sequence of words where row k reads the same as column k.
A hard Google onsite backtracking problem framed around generating mini crossword puzzles. The interviewer checks that you derive the required prefix for the next row from columns already placed and use a trie (or prefix map) to fetch candidates instantly.
What the Interviewer Expects
- Observe that row k must start with the k-th characters of rows 0..k-1
- Index words by prefix using a trie that stores matching words per node
- Backtrack row by row, fetching candidates for the required prefix
- Allow the same word to appear more than once in a square
- Analyze the branching and memory trade-off
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you count squares without listing them?
- How would you add a constraint that diagonals must also be words?
- How would you speed this up with bitsets per position?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: words, distinct lowercase strings all of length L (1 to 5 in the classic version, up to 1000 words). Output: all lists of L words, words may repeat, such that `square[i][j] == square[j][i...
High-Level Design
When rows 0..k-1 are placed, symmetry fixes the first k characters of row k: they must equal square[0][k], square[1][k], ..., square[k-1][k]. So the only question is which words start with tha...