FastPrepSub-matrix Sums
Problems/Sub-matrix Sums

Sub-matrix Sums

IMC Trading logoIMC Trading● MediumNEW GRADOA

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The preserved source Sample0 accepts a 2x2 square sum of 4 at a threshold of 4. This exercise follows the same inclusive comparison: every square of the selected size must have a sum less than or equal to k.

Given an n x n matrix of positive integers and an integer k, return the largest size s such that every s x s sub-matrix has a sum less than or equal to k.

Return 0 when no positive square size satisfies the condition.

Function

maxSubSquareMatrixSumLessThanK(matrix: int[][], k: int) → int

Complete maxSubSquareMatrixSumLessThanK with the following parameters:

  • int[][] matrix: an n x n matrix
  • int k: the maximum allowed sum for every square of the selected size

Examples

Example 1

matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]k = 3return = 0
matrix
012022213332444

The largest sum among the 1 x 1 sub-matrices is 4. Since threshold < 4, no square size satisfies the condition.

1 x 1 sub-matrices

All one by one sub-matrices. Bold entries mark selected cells.
Row / column012
0222
1333
2444

Maximum sum = 4

Example 2

matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]k = 4return = 1
matrix
012022213332444

Every 1 x 1 sub-matrix has sum at most 4, while the maximum 2 x 2 sum is 14.

2 x 2 sub-matrices

Top-left two by two sub-matrix. Bold entries mark selected cells.
Row / column012
0222
1333
2444

Top left

Top-right two by two sub-matrix. Bold entries mark selected cells.
Row / column012
0222
1333
2444

Top right

Bottom-left two by two sub-matrix. Bold entries mark selected cells.
Row / column012
0222
1333
2444

Bottom left

Bottom-right two by two sub-matrix. Bold entries mark selected cells.
Row / column012
0222
1333
2444

Bottom right

The largest 2 x 2 sum is 14.

Example 3

matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]k = 14return = 2
matrix
012022213332444

Every 2 x 2 sub-matrix has sum at most 14, while the 3 x 3 matrix has sum 27.

3 x 3 sub-matrix

Complete three by three sub-matrix. Bold entries mark selected cells.
Row / column012
0222
1333
2444

Sum = 27

Example 4

matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]k = 27return = 3
matrix
012022213332444

The complete 3 x 3 matrix has sum 27, so the entire matrix satisfies the condition.

Example 5

matrix = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]k = 39return = 3
matrix
012301111122222333334444

The maximum square sums for sides 1, 2, 3, 4 are 4, 14, 27, 40. Every 3x3 square is at most 39, but the complete 4x4 sum is 40.

Example 6

matrix = [[4,5],[6,7]]k = 2return = 0
matrix
01045167

The maximum 1x1 sum is 7, greater than 2, so no positive side length can satisfy the every-square condition.

Constraints

  • 1 <= n <= 750
  • 1 <= k <= 10^9
  • 1 <= matrix[i][j] <= 10^7
  • The sum of the entire matrix is at most 10^9.

More IMC Trading Online Assessment questions

  • Earliest Meeting Start on a 24-Hour ClockOA · Seen Nov 2026
  • Grid Paths with Override PassesOA · Seen Oct 2026
  • Weighted LFU CacheOA · Seen Sep 2026
  • Rock JumpingOA · Seen Aug 2026
  • Stack Batch RemovalOA · Seen Aug 2026
  • Chain of CommandOA · Seen Aug 2026
  • Choose ContainersOA · Seen Aug 2026
  • Sub-matrix SumsOA · Seen Jul 2026
  • Can Every Package Fit in a BoxOA · Seen Jul 2026
  • Minimum Space Wasted From PackagingOA · Seen Sep 2025

Similar questions at other companies

  • Largest Sub-GridMathWorks · OAMathWorks interview questions
  • Largest Square Sub-Grid Within a Sum LimitC3.ai · OAC3.ai interview questions
  • Find Minimum Time to Create Beautiful CanvasAmazon · OA
  • Largest Universally Bounded SubgridRoblox · OARoblox interview questions
  • Subscription and Usage-Based Billing CalculatorStripe · OAStripe interview questions
  • Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
  • Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
  • Leaves Remaining After Wind GustsOpenAI · OAOpenAI interview questions
  • Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
  • Minimum Size Subarray SumMicrosoft · OAMicrosoft interview questions

Interview reports that mention this question

  • IMC Trading OA: What It Tests (2026)
  • IMC SWE New Grad OA and Interviews: 58 Experiences (Part 1 of 3)

IMC Trading interview experiences

  • IMC Trading SWE OA and Video: 18 Experiences (Part 3 of 3)
  • IMC Trading SWE OA Questions: 22 Experiences (Part 2 of 3)
All IMC Trading interview questionsSee IMC Trading hiring insights
Saved
class Solution {
    public int maxSubSquareMatrixSumLessThanK(int[][] matrix, int k) {
      // write your code here
    }
}
Checking account…