Maximal Rectangle in a Binary Matrix
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A 200 by 200 grid sounds small until you try every possible rectangle. That's the trap in this Google OA question, reported in September 2026. Brute force checks every pair of corners and then scans the contents, and that blows up fast. The real answer is a reframe: turn each row into a histogram and solve largest rectangle in a histogram. The hinted pattern says breadth-first search, but it doesn't fit here. This is a stack problem. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the working solution in real time, so one bad minute doesn't sink the attempt.
The problem
Given a rectangular binary matrix matrix, return the area of the largest axis-aligned rectangle composed only of 1 values. A rectangle may span any positive number of consecutive rows and columns. Return 0 when the matrix contains no 1. Function maximalRectangle(matrix: int[][]) → 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 = 6 The largest all-one rectangle has height 2 and width 3. Example 2 matrix = [[0]] return = 0 No all-one rectangle exists. Example 3 matrix = [[1,1],[1,1]] return = 4 The entire matrix is an all-one rectangle of area 4. Constraints 1 <= matrix.length <= 200. 1 <= matrix[i].length <= 200. Every row has the same length. Every value is either 0 or 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is building heights row by row. For each column, keep a running count of consecutive 1s ending at the current row, and reset to 0 when you hit a 0. Each row then becomes a histogram, and the best rectangle ending at that row is the largest rectangle in that histogram. Solve that with a monotonic stack: push indices with increasing heights, and when a shorter bar arrives, pop and compute area using the popped height and the width between the new top and the current index. Total work is O(rows * cols), which is easy at 200 by 200. Common pitfalls: forgetting a sentinel bar of height 0 at the end, so the last bars never get popped, and miscalculating the width after a pop. Don't reach for BFS flood fill, because a rectangle isn't a connected blob. StealthCoder is the hedge if the stack logic slips under pressure in the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Maximal Rectangle in a Binary Matrix 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 rectangle. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google 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 Rectangle in a Binary Matrix FAQ
What's the trick for Maximal Rectangle in a Binary Matrix?+
Convert each row into a histogram of consecutive 1s stacked upward, then find the largest rectangle in that histogram. Do it for every row and keep the max. The histogram step uses a monotonic stack, giving O(rows * cols) total time.
Is BFS the right pattern since it was hinted?+
No. BFS or DFS finds connected regions, but a connected region of 1s isn't necessarily a rectangle. You need heights plus a monotonic stack, or a dynamic programming variant tracking left and right bounds per column.
How hard is this one really?+
It's a hard-tier problem, but only because of the reframe. Once you see the histogram, the code is about 25 lines. The 200 by 200 limit means an O(n^2 * m) approach could also pass, so you have a fallback if the stack feels shaky.
What edge cases break solutions?+
A matrix of all 0s must return 0, like the [[0]] example. A single row or single column works as a plain histogram. A fully filled matrix should return rows times columns. Also remember to reset a column's height to 0 when you hit a 0.
How do I prepare for this in 48 hours?+
Write largest rectangle in histogram from scratch until the stack pops feel automatic. Then wrap it in the row-by-row height update. Test with the three given examples, especially the 6 and 4 cases. Skip BFS review, it won't help here.