FastPrepLargest Square Subgrid Under a Sum Limit
Problems/Largest Square Subgrid Under a Sum Limit

Largest Square Subgrid Under a Sum Limit

OpenAI logoOpenAI● MediumOA
QuestionFunctionExamples

Problem statement

Source note: The report clearly described the array, the sum limit, and the requirement that every square of the chosen size satisfy the limit. This version is a close reconstruction of that core task; the original example, function signature, and numeric limits were not included.

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 = 4return = 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.

More OpenAI problems

  • Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
  • Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
All OpenAI interview questionsSee OpenAI hiring insights
Saved
class Solution {
    public int largestSquareSubgrid(int[][] grid, int maxSum) {
      // write your code here
    }

}
Checking account…