FastPrepRange Sum Queries on a Fixed Grid
Problems/Range Sum Queries on a Fixed Grid

Range Sum Queries on a Fixed Grid

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported as the first of two video technical rounds, held one right after the other, in an interview for a software engineering internship for PhD students. After a question on box overlap, with a little over ten minutes left, the interviewer added a second question: the well-known practice problem of summing rectangles of a matrix that never changes, solved with 2D prefix sums. The candidate says they solved it right away. The report gives nothing else about this question: no interface, no examples and no limits.

So these choices are ours. The known problem is a class that is built once from the matrix and then asked for one rectangle per call; here one function gets the matrix and all the queries and returns the sums in order, which is the same work: build once, then answer each query. A query names the rectangle's top-left and bottom-right cells, and both belong to it; row and column numbers start at 0. The grid size and value limits follow the known problem. Queries are capped at 1,000 instead of the known problem's 10^4, to keep the test output small; at this size, adding up each rectangle cell by cell also passes, and the editorial explains what the prefix-sum method saves. All the examples are ours. The box-overlap question asked earlier in the same round and the second round are separate problems.

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

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]
matrix
0123041031251120263

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]
matrix
0120-23-114-52

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]
matrix
0123407-3205

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.length
  • n == matrix[i].length, the same for every row
  • 1 <= m, n <= 200
  • -10^4 <= matrix[i][j] <= 10^4
  • 1 <= queries.length <= 1000
  • queries[i].length == 4
  • 0 <= row1 <= row2 < m
  • 0 <= 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

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[] sumRegions(int[][] matrix, int[][] queries) {
        // Write your code here.
    }
}
Checking account…