Reported July 2026
Amazondynamic programming

Maximal Square

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon reported Maximal Square in July 2026, and it's a classic that hides a simple idea. Strip the matrix story away and it's one question: for each cell, what's the biggest all-ones square that ends here as its bottom-right corner? Answer that for every cell and you're done. It's dynamic programming on a grid, and the recurrence is three neighbors and a min. If you've seen it, it's five minutes of typing. If you haven't, it's easy to overthink. StealthCoder sits invisibly on your screen during the live OA as a safety net if your mind goes blank on the recurrence.

The problem

Given a matrix of characters '0' and '1', return the area of the largest square containing only '1' cells.

Function
maximalSquare(matrix: char[][]) → int

Examples
Example 1
matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
return = 4
The largest all-one square has side length 2.
Example 2
matrix = [["0"]]
return = 0

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: let dp[i][j] be the side length of the largest square of 1s whose bottom-right corner is (i,j). If the cell is '0', dp is 0. Otherwise dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]). Track the max side and return its square. The pitfall is returning the side instead of the area, so square it at the end. Another one is forgetting the input holds characters, not ints, so compare against '1'. Pad the grid with an extra row and column of zeros, or handle the borders explicitly. You can cut space to one row by keeping the previous diagonal value in a variable. Time is O(m*n). If the recurrence slips away mid-assessment, StealthCoder is the hedge that hands you the structure while the proctor sees nothing.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Maximal Square cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximal square. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximal Square FAQ

What's the trick for Maximal Square?+

Use DP where each cell stores the largest square side ending at that cell as the bottom-right corner. If the cell is '1', take 1 plus the min of the top, left, and top-left values. The answer is the max side squared.

How hard is Maximal Square really?+

Medium. The recurrence is short once you know it, but people stumble deriving why min of three neighbors works. A tired brute force over every corner and size also works on small inputs but gets slow on larger grids.

Do I return the side length or the area?+

The area. Example 1 has a side of 2 and returns 4. Track the max side during the DP, then return side * side. An all-zero matrix returns 0, as Example 2 shows.

Can I do it in O(n) extra space?+

Yes. Each row only depends on the previous row and the current one. Keep a single array plus a variable holding the old diagonal value before you overwrite it. Write the 2D version first, then optimize only if you have time.

How do I prep for this in 48 hours?+

Code the 2D DP from scratch twice, then run both examples by hand. Watch the character comparison, the border handling, and the area versus side mistake. Then do a few other grid DP problems so the recurrence feels familiar under pressure.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it