Reported July 2026
OpenAIprefix sum

Largest Square Subgrid Under a Sum Limit

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

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

The whole OpenAI problem from July 2026 hinges on one data structure: a 2D prefix sum table. Build it once and any k x k square sum costs O(1) to read. You're given a grid and a maxSum, and you return the biggest side length where every square of that size stays at or under the limit. It looks like a brute-force mess until the prefix table clicks. If you blank on the setup during the live OA, StealthCoder runs invisibly on screen as a safety net. But you can own this one.

The problem

You are given a two-dimensional integer array grid and an integer maxSum.
For a side length k, consider every contiguous k x k square subgrid. The side length is valid when the sum of every such square is at most maxSum.
Return the maximum valid side length.

Function
largestSquareSubgrid(grid: int[][], maxSum: int) → int

Examples
Example 1
grid = [[1,1,1],[1,1,1],[1,1,1]]
maxSum = 4
return = 2
Every 2 x 2 square has sum 4, while the only 3 x 3 square has sum 9. Therefore the largest valid side length is 2.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build a 2D prefix sum so any square sum is four lookups. Then the key insight: validity is monotonic only if the values are non-negative. If every k x k square is valid, every smaller size is valid too, so you can binary search on k. Check each k by scanning all squares and confirming none exceed maxSum. That gives O(m*n*log(min(m,n))). The pitfall is the input says integers, so negatives may break monotonicity. Read the constraints before you commit to binary search. If negatives are allowed, a linear scan over k from 1 upward, tracking the largest valid size, is the safe fallback. Also watch the off-by-one in the prefix table. Use a (m+1) x (n+1) array padded with zeros. If the indexing trips you mid-assessment, StealthCoder is the hedge that hands you a clean solution while you keep typing.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Largest Square Subgrid Under a Sum Limit 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum side length of a square with sum less than or equal to threshold. If you have time before the OA, drill that.

⏵ The honest play

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 Subgrid Under a Sum Limit FAQ

What's the trick in the OpenAI largest square subgrid problem?+

Use a 2D prefix sum so each k x k square sum is O(1). Then binary search on side length k, since smaller squares stay valid when larger ones are, assuming non-negative values. Check every square of size k against maxSum.

Does binary search always work here?+

Only if the grid values are non-negative. With negatives, a bigger valid square doesn't guarantee smaller ones are valid. Check the constraints first. If negatives appear, loop k upward and test each size directly.

How hard is this really?+

Medium. The prefix sum is standard, and the binary search layer is the only extra step. Most mistakes come from indexing, not the idea. Pad the prefix array with a zero row and column to avoid edge cases.

What's the time complexity?+

Building the prefix table is O(m*n). Each check of size k scans O(m*n) squares. With binary search over k up to min(m,n), total is O(m*n*log(min(m,n))). A linear scan over k would be O(m*n*min(m,n)).

How do I prepare in 48 hours?+

Write the 2D prefix sum from memory until the formula is automatic: add top and left, subtract the overlap. Then practice a binary-search-on-answer template. Test on the 3x3 all-ones example with maxSum 4, which should return 2.

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

OA at OpenAI?
Invisible during screen share
Get it