Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Maximal Square
Problem
You are given a binary matrix of '0' and '1' characters. Find the largest square sub-block consisting entirely of '1' and return its area.
1 <= rows, cols <= 300- Each cell is the character
'0'or'1'.
Examples
matrix = [["1","1","0","1","1"],["1","1","1","1","0"],["0","1","1","1","1"],["1","1","1","0","0"]]→4. A 2x2 block of ones sits in rows 0-1, columns 0-1.matrix = [["1","0"],["0","1"]]→1. No two adjacent ones form a square larger than a single cell.matrix = [["0"]]→0. No ones at all.
Hints
- Think about squares by their bottom-right corner. If a square of side
kends at a cell, what must be true of the cells above, to the left, and diagonally up-left? - Three overlapping squares of side
k-1ending at those three neighbours, plus a one in the current cell, guarantee a square of sidek. dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])when the cell is a one, else 0. Answer ismax(dp)².
Last reviewed 8 September 2026
Locked
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.
Secure payment by Stripe · instant access · refund questions: [email protected]
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.