Largest Square of Ones
Reported by candidates from OpenAI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole problem hinges on one 2D DP table, and OpenAI put it in front of candidates in September 2026. It's Largest Square of Ones: given a binary matrix, return the side length of the biggest square made only of 1s. If you've got the OA coming up, this is a matrix dynamic programming question dressed in plain clothes. Once you see the recurrence, it's ten lines. If you blank on it, StealthCoder is the invisible safety net running during the live OA, reading the problem and giving you the solution. But you should walk in knowing the trick first.
The problem
Given a rectangular binary matrix matrix, return the side length of the largest square submatrix composed only of 1 values. If the matrix contains no 1, return 0. Function findLargestSquare(matrix: int[][]) → int Examples Example 1 matrix = [[1,1,0,1,0],[1,1,1,1,1],[0,0,1,1,1],[0,0,1,1,1],[1,1,0,0,0]] return = 3 The rows and columns from index 1 through 3 contain a 3 x 3 square of ones. Example 2 matrix = [[0,0],[0,0]] return = 0 No cell contains 1. Example 3 matrix = [[1]] return = 1 The single cell is itself a square of side length 1. Constraints 1 <= matrix.length <= 500. 1 <= matrix[i].length <= 500. Every row has the same length. Every value is either 0 or 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a table where dp[i][j] is the side of the largest all-ones square whose bottom-right corner is cell (i,j). If the cell is 0, dp is 0. If it's 1, dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]). Track the max as you go and return it. That's O(m*n) time on a grid up to 500 by 500, which is fine. The common pitfall is returning the area instead of the side length, since the prompt asks for the side. Another is mishandling the first row and column, where the dp value is just the cell value. You can compress to one row of memory, but only if you save the diagonal value before overwriting it. If the recurrence slips away mid-OA, StealthCoder can hand you the working version while the proctor sees nothing.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Largest Square of Ones 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. If you're reading this with an OA window open, you're who this was built for.
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 OpenAI's OA.
OpenAI reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Largest Square of Ones FAQ
What's the trick to Largest Square of Ones?+
Define dp[i][j] as the largest square side ending at that cell as the bottom-right corner. When the cell is 1, take 1 plus the minimum of the top, left, and top-left neighbors. Track the maximum value seen. That min is the whole insight.
How hard is this OpenAI question really?+
It's medium. The recurrence is short, but you have to see it. Brute force checking every square works on small inputs but gets slow at 500 by 500. If you know the standard DP, you finish fast and spend your time on edge cases.
Do I return the area or the side length?+
The side length. Example 1 returns 3, not 9. Many people compute the area by habit from the classic version of this problem. Read the function signature and examples, then return the max dp value directly without squaring it.
Can I reduce the space below O(m*n)?+
Yes. Keep a single row, plus one variable holding the previous diagonal value. Before you overwrite dp[j], save it as the next diagonal. It's easy to get wrong under pressure, so write the full 2D version first, then optimize if there's time.
How do I prepare for this in 48 hours?+
Code the dp recurrence from memory twice, once with a full table and once with a rolling row. Then test the edge cases: a 1x1 grid, an all-zero grid, a single row, and a single column. That covers what this question can throw at you.