Quick Overview

Given a fixed integer grid and up to 100,000 rectangle queries, return the sum of every inclusive rectangle in query order. Tests whether a candidate avoids rescanning cells for each query, handles negative values and sums beyond 32 bits, and gets the rectangle boundaries right.

Answer Many Rectangle Sum Queries on a Fixed Integer Grid

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an m × n grid of integers that never changes, and a list of rectangle queries. Each query names a rectangle by its top-left and bottom-right cells and asks for the sum of every cell inside it, boundaries included. Return the answers in the order the queries are given. ### Function Signature ```python def range_sums(matrix: List[List[int]], queries: List[List[int]]) -> List[int]: ``` ### Rules - `queries[j] = [r1, c1, r2, c2]`: (r1, c1) is the top-left cell and (r2, c2) the bottom-right cell, 0-indexed by row then column, with r1 ≤ r2 and c1 ≤ c2. - `answer[j]` is the sum of `matrix[r][c]` over all r1 ≤ r ≤ r2 and c1 ≤ c ≤ c2. - The result has exactly one value per query, in query order. ### Constraints - 1 ≤ m, n ≤ 500, and every row has length n. - -10^5 ≤ matrix[r][c] ≤ 10^5. - 1 ≤ len(queries) ≤ 10^5. - 0 ≤ r1 ≤ r2 < m and 0 ≤ c1 ≤ c2 < n for every query. - A sum can reach 2.5 × 10^10 in absolute value, which is beyond 2^31 - 1 (use 64-bit integers in a typed language) and well within ±2^53. ```hint Count the work first Estimate the total cost of adding up each rectangle cell by cell at the largest grid size and query count before you pick an approach. ``` ### Examples Example 1 ``` matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] queries = [[0, 0, 1, 1], [1, 1, 2, 2], [0, 0, 2, 2], [2, 0, 2, 2]] output = [12, 28, 45, 24] ``` The first query covers 1 + 2 + 4 + 5 = 12, and the last covers the bottom row, 7 + 8 + 9 = 24. Example 2 ``` matrix = [[-1, 4], [2, -3]] queries = [[0, 0, 0, 0], [0, 1, 1, 1], [0, 0, 1, 1]] output = [-1, 1, 2] ``` A single-cell query returns that cell. The second query is the right column, 4 + (-3) = 1, and the third is the whole grid.

Overview: Given a fixed integer grid and up to 100,000 rectangle queries, return the sum of every inclusive rectangle in query order. Tests whether a candidate avoids rescanning cells for each query, handles negative values and sums beyond 32 bits, and gets the rectangle boundaries right.

Read the full Google Software Engineer interview experience this question came from

You are given an m × n grid of integers `matrix` that never changes, and a list of rectangle queries `queries`. Each query names a rectangle by its top-left and bottom-right cells and asks for the sum of every cell inside it, boundaries included. Return the answers in the order the queries are given. - `queries[j] = [r1, c1, r2, c2]`: (r1, c1) is the top-left cell and (r2, c2) the bottom-right cell, 0-indexed by row then column, with r1 ≤ r2 and c1 ≤ c2. - `answer[j]` is the sum of `matrix[r][c]` over all r1 ≤ r ≤ r2 and c1 ≤ c ≤ c2. - The result has exactly one value per query, in query order. A single answer can reach 2.5 × 10^10 in absolute value, which exceeds 2^31 - 1. Use 64-bit integers in typed languages: the Java method returns `long[]` and the C++ method returns `std::vector<long long>`. Every answer stays well within ±2^53. ### Example 1 ``` Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] queries = [[0, 0, 1, 1], [1, 1, 2, 2], [0, 0, 2, 2], [2, 0, 2, 2]] Output: [12, 28, 45, 24] ``` The first query covers 1 + 2 + 4 + 5 = 12, the second covers 5 + 6 + 8 + 9 = 28, the third is the whole grid (45), and the last covers the bottom row, 7 + 8 + 9 = 24. ### Example 2 ``` Input: matrix = [[-1, 4], [2, -3]] queries = [[0, 0, 0, 0], [0, 1, 1, 1], [0, 0, 1, 1]] Output: [-1, 1, 2] ``` A single-cell query returns that cell. The second query is the right column, 4 + (-3) = 1, and the third is the whole grid. ### Constraints - 1 ≤ m, n ≤ 500, and every row has length n. - -10^5 ≤ matrix[r][c] ≤ 10^5. - 1 ≤ len(queries) ≤ 10^5. - 0 ≤ r1 ≤ r2 < m and 0 ≤ c1 ≤ c2 < n for every query. - A sum can reach 2.5 × 10^10 in absolute value, which is beyond 2^31 - 1 (use 64-bit integers in a typed language) and well within ±2^53.

Constraints

  • 1 ≤ m, n ≤ 500, and every row has length n.
  • -10^5 ≤ matrix[r][c] ≤ 10^5.
  • 1 ≤ len(queries) ≤ 10^5.
  • 0 ≤ r1 ≤ r2 < m and 0 ≤ c1 ≤ c2 < n for every query.
  • A sum can reach 2.5 × 10^10 in absolute value, which is beyond 2^31 - 1 (use 64-bit integers in a typed language) and well within ±2^53.

Examples

Input: ([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[0, 0, 1, 1], [1, 1, 2, 2], [0, 0, 2, 2], [2, 0, 2, 2]])

Expected Output: [12, 28, 45, 24]

Explanation: Example 1 from the statement: a top-left 2 x 2 block, the bottom-right 2 x 2 block, the whole grid and the bottom row.

Input: ([[-1, 4], [2, -3]], [[0, 0, 0, 0], [0, 1, 1, 1], [0, 0, 1, 1]])

Expected Output: [-1, 1, 2]

Explanation: Example 2 from the statement: a single negative cell, the right column 4 + (-3), and the whole grid.

Hints

  1. Estimate the total cost of adding up each rectangle cell by cell at the largest grid size (500 × 500) and query count (10^5) before you pick an approach.
  2. The grid never changes between queries, so work done once on the grid can be reused by every query.
  3. A single answer can reach 2.5 × 10^10 in absolute value, so check that your integer type holds it, and test rectangles that start at row 0 or column 0.

Rectangle Sum Queries on a Fixed Grid

mediumGoogleSoftware Engineer

You are given an m × n grid of integers matrix that never changes, and a list of rectangle queries queries. Each query names a rectangle by its top-left and bottom-right cells and asks for the sum of every cell inside it, boundaries included. Return the answers in the order the queries are given.

  • queries[j] = [r1, c1, r2, c2]: (r1, c1) is the top-left cell and (r2, c2) the bottom-right cell, 0-indexed by row then column, with r1 ≤ r2 and c1 ≤ c2.
  • answer[j] is the sum of matrix[r][c] over all r1 ≤ r ≤ r2 and c1 ≤ c ≤ c2.
  • The result has exactly one value per query, in query order.

A single answer can reach 2.5 × 10^10 in absolute value, which exceeds 2^31 - 1. Use 64-bit integers in typed languages: the Java method returns long[] and the C++ method returns std::vector<long long>. Every answer stays well within ±2^53.

Example 1

Input:  matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        queries = [[0, 0, 1, 1], [1, 1, 2, 2], [0, 0, 2, 2], [2, 0, 2, 2]]
Output: [12, 28, 45, 24]

The first query covers 1 + 2 + 4 + 5 = 12, the second covers 5 + 6 + 8 + 9 = 28, the third is the whole grid (45), and the last covers the bottom row, 7 + 8 + 9 = 24.

Example 2

Input:  matrix = [[-1, 4], [2, -3]]
        queries = [[0, 0, 0, 0], [0, 1, 1, 1], [0, 0, 1, 1]]
Output: [-1, 1, 2]

A single-cell query returns that cell. The second query is the right column, 4 + (-3) = 1, and the third is the whole grid.

Constraints

  • 1 ≤ m, n ≤ 500, and every row has length n.
  • -10^5 ≤ matrix[r][c] ≤ 10^5.
  • 1 ≤ len(queries) ≤ 10^5.
  • 0 ≤ r1 ≤ r2 < m and 0 ≤ c1 ≤ c2 < n for every query.
  • A sum can reach 2.5 × 10^10 in absolute value, which is beyond 2^31 - 1 (use 64-bit integers in a typed language) and well within ±2^53.

Examples

Example 1
Input
([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[0, 0, 1, 1], [1, 1, 2, 2], [0, 0, 2, 2], [2, 0, 2, 2]])
Output
[12, 28, 45, 24]
Notes
Example 1 from the statement: a top-left 2 x 2 block, the bottom-right 2 x 2 block, the whole grid and the bottom row.
Example 2
Input
([[-1, 4], [2, -3]], [[0, 0, 0, 0], [0, 1, 1, 1], [0, 0, 1, 1]])
Output
[-1, 1, 2]
Notes
Example 2 from the statement: a single negative cell, the right column 4 + (-3), and the whole grid.

Constraints

  • •1 ≤ m, n ≤ 500, and every row has length n.
  • •-10^5 ≤ matrix[r][c] ≤ 10^5.
  • •1 ≤ len(queries) ≤ 10^5.
  • •0 ≤ r1 ≤ r2 < m and 0 ≤ c1 ≤ c2 < n for every query.
  • •A sum can reach 2.5 × 10^10 in absolute value, which is beyond 2^31 - 1 (use 64-bit integers in a typed language) and well within ±2^53.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def range_sums(matrix: list[list[int]], queries: list[list[int]]) -> list[int]:
Sample call
range_sums([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[0, 0, 1, 1], [1, 1, 2, 2], [0, 0, 2, 2], [2, 0, 2, 2]])
Language
Case 1
Input
([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[0, 0, 1, 1], [1, 1, 2, 2], [0, 0, 2, 2], [2, 0, 2, 2]])
Expected
[12, 28, 45, 24]
Case 2
Input
([[-1, 4], [2, -3]], [[0, 0, 0, 0], [0, 1, 1, 1], [0, 0, 1, 1]])
Expected
[-1, 1, 2]

Browse More Questions

Show the approach

Approach

Build a prefix table P with one extra row and one extra column of zeros, where P[i][j] is the sum of matrix[r][c] over all r < i and c < j. Fill it row by row: keep a running sum of the current matrix row and set P[r + 1][c + 1] = P[r][c + 1] + running, which adds the part of row r up to column c to the block above it.

For a query [r1, c1, r2, c2], P[r2 + 1][c2 + 1] covers rows 0 to r2 and columns 0 to c2. Subtracting P[r1][c2 + 1] removes the rows above r1, and subtracting P[r2 + 1][c1] removes the columns left of c1. The block above r1 and left of c1 was removed twice, so adding P[r1][c1] back leaves exactly the inclusive rectangle. Answers are appended in query order, so the result has one value per query in the given order, duplicates included.

The zero padding row and column let rectangles that start at row 0 or column 0 use the same formula with no special branch. A single-cell query reduces to that cell, and one-row or one-column grids follow the same steps. Table entries and answers can reach 2.5 × 10^10 in absolute value, so Java and C++ store them as 64-bit integers, and JavaScript keeps them in a Float64Array, which holds every integer of that size exactly.

Building the table takes O(m * n) time and each query takes O(1), compared with up to 500 * 500 * 10^5 cell additions when each rectangle is summed directly.

Time complexity:
O(m * n + q)
Space complexity:
O(m * n + q)