FastPrepSub-Matrix Sum Queries
Problems/Sub-Matrix Sum Queries

Sub-Matrix Sum Queries

Hudson River Trading logoHudson River Trading● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported as one of four coding questions in a Hudson River Trading algorithm developer intern online assessment. The candidate recalled only that it used prefix sums over a grid and said knowing the formula helps.

The report gives no statement, inputs or limits. Here the task is answering many rectangle-sum queries on a grid. The input format, examples and limits are our own choices.

Our version is about 40% the same as the reported question.

Given an m x n integer grid matrix and a list of queries, return an array whose i-th value is the sum of all cells in the rectangle described by queries[i].

Each query is [r1, c1, r2, c2]. Its rectangle has top-left cell (r1, c1) and bottom-right cell (r2, c2), both included. Rows and columns are numbered from 0.

Function

submatrixSums(matrix: int[][], queries: int[][]) → long[]

Examples

Example 1

matrix = [[2,7,1,4],[3,5,0,6],[8,1,9,2],[4,3,6,1]]queries = [[1,1,2,2],[0,0,3,3],[0,3,3,3]]return = [15,62,13]
matrix
012302714135062819234361

Rows 1 to 2 and columns 1 to 2 hold 5 + 0 + 1 + 9 = 15. The whole grid sums to 62. The last query is column 3 in all four rows, 4 + 6 + 2 + 1 = 13.

Example 2

matrix = [[-2,7],[4,-5]]queries = [[0,0,0,0],[0,0,1,1],[1,0,1,1]]return = [-2,4,-1]
matrix
010-2714-5

The first query is one cell, -2. The whole grid is -2 + 7 + 4 - 5 = 4. The bottom row is 4 - 5 = -1.

Constraints

  • m == matrix.length, n == matrix[i].length
  • 1 <= m, n <= 300
  • -10^5 <= matrix[i][j] <= 10^5
  • 1 <= queries.length <= 10^5
  • queries[i] = [r1, c1, r2, c2] with 0 <= r1 <= r2 < m and 0 <= c1 <= c2 < n

More Hudson River Trading Online Assessment questions

  • Count Palindrome Addition ChoicesOA · Seen Nov 2026
  • Count Multiples of Three with Two SevensOA · Seen Oct 2026
  • Recursively Expanded Shell Command CountsOA · Seen Oct 2026
  • Rotate Matrix Regions Around the DiagonalsOA · Seen Oct 2026
  • Maximum Diamond SumOA · Seen Oct 2026
  • Smallest Most-Lit Street PositionOA · Seen Oct 2026
  • Rearrange String by Alternating EndsOA · Seen Sep 2026
  • Most Items Within Budget from a Start IndexOA · Seen Sep 2026
  • Count Fancy NumbersOA · Seen Sep 2026
  • Count Pieces After Othello MovesOA · Seen Sep 2026

Similar questions at other companies

  • Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
  • Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
  • Largest Square Sub-Grid Within a Sum LimitC3.ai · OAC3.ai interview questions
  • Maximum Sum RectangleDeloitte · OADeloitte interview questions
  • Range Sum Queries on a Fixed GridGoogle · ONSITE INTERVIEWGoogle interview questions
  • Centered K-by-K Submatrix SumsCitadel · ONSITE INTERVIEWCitadel interview questions
  • Restaurant Delivery HeatmapDoorDash · ONSITE INTERVIEWDoorDash interview questions
  • Matrix Region and Ring SumsNVIDIA · PHONE SCREENNVIDIA interview questions
  • Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
  • Number of Submatrices That Sum to TargetWhatNot · ONSITE INTERVIEWWhatNot interview questions

Interview reports that mention this question

  • Hudson River Trading OA · 2023

Hudson River Trading interview experiences

  • Hudson River Trading Data Science Interviews: 2 Experiences
  • Hudson River Trading interviews · 2021
  • Hudson River Trading OA · 2021
  • Hudson River Trading interviews · 2022
  • Hudson River Trading OA · 2022
All Hudson River Trading interview questionsSee Hudson River Trading hiring insights
Saved
class Solution {
    public long[] submatrixSums(int[][] matrix, int[][] queries) {
        // Write your code here.
    }
}
Checking account…