Range Sum Queries on a Fixed Grid
Updated
Problem statement
You are given matrix, a rectangular grid of integers that stays the same for the whole run, and a list of rectangle queries on it. For each query, return the total of the numbers inside its rectangle, in the same order as the queries.
Each query is a list of four integers, [row1, col1, row2, col2]. The cell in row row1 and column col1 is the top-left corner of the rectangle, and the cell in row row2 and column col2 is its bottom-right corner. Both corner cells are part of the rectangle. Row and column numbers start at 0.
Every total fits in a 32-bit signed integer.
Function
sumRegions(matrix: int[][], queries: int[][]) → int[]Examples
Example 1
matrix = [[4,1,0,3],[2,5,1,1],[0,2,6,3]]queries = [[1,1,2,2],[0,0,2,3],[2,3,2,3]]return = [14,28,3]The first rectangle covers rows 1 to 2 and columns 1 to 2: 5 + 1 + 2 + 6 = 14. The second covers the whole grid: 8 + 9 + 11 = 28. The third is the single cell in row 2, column 3, which holds 3.
Example 2
matrix = [[-2,3,-1],[4,-5,2]]queries = [[0,0,1,0],[0,1,1,2],[1,0,1,2]]return = [2,-1,1]Column 0 gives -2 + 4 = 2. Columns 1 to 2 of both rows give 3 + (-1) + (-5) + 2 = -1. Row 1 alone gives 4 + (-5) + 2 = 1.
Example 3
matrix = [[7,-3,2,0,5]]queries = [[0,1,0,3],[0,0,0,4],[0,4,0,4],[0,1,0,3]]return = [-1,11,5,-1]The grid has one row. Columns 1 to 3 give -3 + 2 + 0 = -1, the whole row gives 11, and column 4 alone gives 5. The last query repeats the first one; the grid has not changed, so the answer is -1 again.
Constraints
m == matrix.lengthn == matrix[i].length, the same for every row1 <= m, n <= 200-10^4 <= matrix[i][j] <= 10^41 <= queries.length <= 1000queries[i].length == 40 <= row1 <= row2 < m0 <= col1 <= col2 < n
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Centered K-by-K Submatrix SumsCitadel · ONSITE INTERVIEWCitadel interview questions
- Restaurant Delivery HeatmapDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Number of Submatrices That Sum to TargetWhatNot · ONSITE INTERVIEWWhatNot interview questions
- 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
- Sub-Matrix Sum QueriesHudson River Trading · OAHudson River Trading interview questions
- Largest Square Sub-Grid Within a Sum LimitC3.ai · OAC3.ai interview questions
- Matrix Region and Ring SumsNVIDIA · PHONE SCREENNVIDIA interview questions
- Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
- Maximum Sum RectangleDeloitte · OADeloitte interview questions