Maximal Square
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Salesforce reported Maximal Square in July 2026, and the trap is the one that makes a brute-force answer look right until it isn't. Return the area of the biggest all-ones square in a binary grid. Example 3 is a single '0' cell, which should give 0, and a lot of solutions that start the answer at 1 or return the side instead of the area fail right there. It's a dynamic programming problem on a matrix, and the recurrence is short once you see it. If your head goes blank mid-assessment, StealthCoder sits invisibly on your screen as a safety net. Read the pattern below first.
The problem
Given an m x n binary matrix containing the characters '0' and '1', return the area of the largest square whose cells are all '1'. 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, so its area is 4. Example 2 matrix = [["0","1"],["1","0"]] return = 1 Each '1' cell forms a square of side length 1, and no larger all-one square exists. Example 3 matrix = [["0"]] return = 0 The matrix contains no '1' cell, so the maximum area is 0. Constraints m == matrix.length n == matrix[i].length 1 <= m, n <= 300 Each matrix[i][j] is either '0' or '1'.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is dp[i][j] = the side length of the largest all-ones square whose bottom-right corner is (i,j). If the cell is '1', dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]). If it's '0', it's 0. Track the max side, then return side * side. That's O(m*n) time, and you can cut space to one row. Common pitfalls: returning the side instead of the area, forgetting the cells are characters and not ints, and mishandling the first row and column, where dp is just 1 or 0. Start the max at 0 so the all-zero grid in Example 3 returns 0. With m and n up to 300, brute force checking every square is too slow. If you freeze during the live OA, StealthCoder can hand you this recurrence as a hedge.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as maximal square. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Salesforce's OA.
Salesforce reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximal Square FAQ
What's the trick to Maximal Square?+
Use DP where each cell stores the side of the largest all-ones square ending at that cell as its bottom-right corner. For a '1', take 1 plus the min of the top, left, and top-left neighbors. Square the best side at the end. That min is the whole insight.
How hard is this problem really?+
It's medium. The code is about ten lines once you know the recurrence. The hard part is seeing that a square is limited by its weakest neighbor. If you've seen one grid DP problem, you can get this in one pass.
What edge cases should I test?+
Test a single '0' cell (answer 0), a single '1' cell (answer 1), and a grid like [[0,1],[1,0]] where the answer is 1. Also try a single row or column, and an all-ones grid. Make sure you return area, not side length.
Can I do it with less memory?+
Yes. Each row only depends on the previous row, so keep one array plus a variable for the old top-left value. That drops space from O(m*n) to O(n). Write the 2D version first, since it's easier to get right, and only optimize if you have time.
How do I prepare for this in 48 hours?+
Write the 2D DP from scratch twice without looking. Then hand-trace Example 1 to see why the min of three neighbors works. Also practice the first row and column handling, since that's where most bugs show up. That's enough for this pattern.