Matrix Region and Ring Sums
Updated
Problem statement
You are given an integer matrix and a list of queries. Return one sum for each query.
Each query is [type, row1, col1, row2, col2]. It describes a rectangle from row row1 to row row2 and from column col1 to column col2, with both ends included. You can count on row1 <= row2 and col1 <= col2.
type = 0: return the sum of every cell in the rectangle.type = 1: return the sum of the rectangle's ring. The ring is its outer border, one cell thick. Count each corner once. If the rectangle is one row or one column, every cell in it is on the ring.
For the ring follow-up, add up the border cells directly. Do not get it by taking the full rectangle's sum and subtracting the inner rectangle's sum. The matrix never changes between queries. Preprocess it once so that every type-0 rectangle query takes constant time.
Function
regionAndRingSums(matrix: int[][], queries: int[][]) → int[]Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]queries = [[0,0,0,2,2],[1,0,0,2,2],[1,1,1,1,1]]return = [45,40,5]The first query covers the whole matrix, so its sum is 45. The second query asks for the ring of that same rectangle. Only the center value 5 is not on the border, so the ring sums to 40. The third query is a single cell. A one-cell ring is just that cell, so the answer is the center value again.
Example 2
matrix = [[-2,4,1],[3,-5,6]]queries = [[0,0,1,1,2],[1,0,0,1,2]]return = [6,7]The right two columns sum to 6. A two-row rectangle has no interior, so its ring includes all six values and sums to 7.
Example 3
matrix = [[1,-1,2,4]]queries = [[1,0,1,0,2],[0,0,0,0,3]]return = [1,6]The one-row ring uses -1 and 2. The full row sums to 6.
Constraints
1 <= matrix.length, matrix[0].length <= 200-10000 <= matrix[r][c] <= 100000 <= queries.length <= 10000- Every query has five integers,
typeis0or1, and its inclusive corners are within the matrix.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
- Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Range Sum Queries on a Fixed GridGoogle · ONSITE INTERVIEWGoogle interview questions
- Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
- Centered K-by-K Submatrix SumsCitadel · ONSITE INTERVIEWCitadel interview questions
- Restaurant Delivery HeatmapDoorDash · ONSITE INTERVIEWDoorDash 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
- Number of Submatrices That Sum to TargetWhatNot · ONSITE INTERVIEWWhatNot interview questions
- Maximum Sum RectangleDeloitte · OADeloitte interview questions