Word Squares
Updated
Problem statement
Given an array words whose strings all have the same length length, return every word square that can be formed.
A word square contains exactly length rows. For every valid pair of indices row and column, the character at row row, column column must equal the character at row column, column row.
- Every row must be a word from
words. - The same word may be used more than once in one square.
- Return an empty array when no square can be formed.
- Return the squares in lexicographic order by their row sequences.
Function
findWordSquares(words: String[]) → String[][]Examples
Example 1
words = ["ball","area","leap","lead","lady"]return = [["ball","area","lead","lady"]]The rows and columns both spell ball, area, lead, and lady in the same order.
Example 2
words = ["abat","baba","atan","atal"]return = [["baba","abat","baba","atal"],["baba","abat","baba","atan"]]Both results are valid squares. The word baba is reused, which is allowed.
Example 3
words = ["abc","def","ghi"]return = []No ordering of three provided words satisfies the row-column equality rule.
Constraints
1 <= words.length <= 2001 <= words[i].length <= 5- All words have the same length and contain only lowercase English letters.
- The input words are distinct.
- The total number of returned word squares is at most
1000.
More InMobi Onsite Interview questions
- Russian Doll EnvelopesONSITE INTERVIEW · Seen Jul 2026
- Minimum and Maximum with Optimal ComparisonsONSITE INTERVIEW · Seen Dec 2025
- Minimum Edges to Connect All ComponentsONSITE INTERVIEW · Seen Dec 2025
- Shortest Path Visiting All NodesONSITE INTERVIEW · Seen Sep 2025
- Maximum Sum of Non-Adjacent Tree NodesONSITE INTERVIEW · Seen Jan 2022
- Maximum Length Pair ChainONSITE INTERVIEW · Seen Jul 2021
- Top View of a Binary TreeONSITE INTERVIEW · Seen Jul 2021
- Maximum XOR Between Two ArraysONSITE INTERVIEW · Seen May 2021
- Trapping Rain WaterONSITE INTERVIEW · Seen May 2021
- Validate Binary Search TreeONSITE INTERVIEW · Seen May 2021
Similar questions at other companies
- Word Break IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Word Search IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Word Break IIGoogle · ONSITE INTERVIEWGoogle interview questions
- Word Break IIWex · PHONE SCREEN
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- All Balanced Strings from Minimum Parenthesis InsertionsRubrik · ONSITE INTERVIEWRubrik interview questions
- Design Search Autocomplete SystemRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Remove Invalid ParenthesesMygate · ONSITE INTERVIEWMygate interview questions
- Extract Company Tickers from NewsSusquehanna International Group (SIG) · ONSITE INTERVIEWSusquehanna International Group (SIG) interview questions