Sub-Matrix Sum Queries
Updated
Problem statement
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]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]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].length1 <= m, n <= 300-10^5 <= matrix[i][j] <= 10^51 <= queries.length <= 10^5queries[i] = [r1, c1, r2, c2]with0 <= r1 <= r2 < mand0 <= 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