Largest Sub-Grid
Reported by candidates from Mathwork's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You're facing a matrix sum problem from MathWorks in November 2025. The task looks straightforward: find the largest square sub-grid where the sum doesn't exceed maxSum. But the trick isn't BFS. It's a 2D prefix sum setup that lets you query any rectangular region in constant time, then binary search or iterate on the side length. Candidates often waste time on naive nested loops. StealthCoder has the pattern locked if you blank.
The problem
Given a square integer matrix grid and an integer maxSum, find the maximum side length of a square sub-grid whose total sum is at most maxSum. If no square sub-grid satisfies the condition, return 0. Every 1 x 1 square in the last row sums to 4, but no larger square stays within the limit. Under the prompt's example, the maximum valid size is 0.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The real pattern here is prefix-sum combined with iteration on side length. Build a 2D prefix array so you can compute any sub-grid sum in O(1). Then iterate side lengths from 1 upward, checking all positions in the grid for each size until you find one that exceeds maxSum. The false hint toward BFS is a red herring. The problem is pure 2D range-sum query. Common miss: not building the prefix array correctly, or checking sums inefficiently. StealthCoder's primary value on the live OA is catching that setup mistake and feeding you the correct prefix indexing in seconds.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Largest Sub-Grid 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 Mathwork's OA.
Mathwork 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.
Largest Sub-Grid FAQ
Is this really a BFS problem?+
No. BFS is a distractor. This is a 2D prefix-sum problem where you query sub-grid sums in constant time. Once you have that, you iterate side lengths and check if any k by k square fits within maxSum.
What's the time complexity I should aim for?+
O(n^3) or O(n^2 log n) if you binary search the side length. With prefix sums, each query is O(1), so the bottleneck is iterating all positions and side lengths. This is acceptable for most grid sizes.
How do I build the 2D prefix sum correctly?+
prefix[i][j] = grid[i][j] + prefix[i-1][j] + prefix[i][j-1] - prefix[i-1][j-1]. Then the sum of a rectangle from (r1, c1) to (r2, c2) is prefix[r2][c2] - prefix[r1-1][c2] - prefix[r2][c1-1] + prefix[r1-1][c1-1].
Should I binary search the side length or iterate linearly?+
Linear iteration is simpler and avoids off-by-one errors under time pressure. Iterate k from n down to 1, or 1 up to n. Binary search is faster but only matters if n is large and you're optimizing.
What if no square fits within maxSum?+
Check every 1x1 cell first. If even one cell exceeds maxSum, return 0. Otherwise, the answer is at least 1. Edge case: maxSum is negative and all cells are positive, return 0.