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
- 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.
- The grid never changes between queries, so work done once on the grid can be reused by every query.
- 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.