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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

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
trie
backtracking
word squares
prefix lookup
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported