Reported July 2026
Ripplingprefix sum

Matrix Region Sum

Reported by candidates from Rippling's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Rippling OA. Under 2s to a working solution.
Founder's read

The edge case that breaks a naive solution on this Rippling OA, reported July 2026, is the clipped border. Interior cells are easy. Corner and edge cells shrink the window, and one off-by-one in your bounds gives you wrong sums on the exact rows the examples show. The hinted pattern says breadth-first-search, but this is really a 2D prefix sum problem. If you brute force every window on a 1000 x 1000 grid with r up to 1000, you blow up. If you blank under the clock, StealthCoder is the safety net running invisibly during the live OA.

The problem

Given an integer matrix matrix and a non-negative region radius r, construct an output matrix of the same size.
For each cell (row, col), sum every input cell (i, j) satisfying:
row - r <= i <= row + r
col - r <= j <= col + r
Only coordinates inside the matrix are included. The center cell is included in its own sum.

Function
matrixRegionSum(matrix: int[][], r: int) → int[][]

Examples
Example 1
matrix = [[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]]
r = 1
return = [[4,6,6,6,4],[6,9,9,9,6],[6,9,9,9,6],[4,6,6,6,4]]
The top-left cell sees the clipped 2 x 2 region and has sum 4. Interior cells see a full 3 x 3 region and have sum 9.
Example 2
matrix = [[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]]
r = 2
return = [[9,12,15,12,9],[12,16,20,16,12],[12,16,20,16,12],[9,12,15,12,9]]
With radius 2, the center columns include all five input columns while edge cells use clipped regions.

Constraints
1 <= matrix.length, matrix[0].length <= 1000
0 <= r <= 1000
All output sums fit in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build a 2D prefix sum with an extra row and column of zeros. Then for each cell (row, col), clamp the bounds: r1 = max(0, row - r), c1 = max(0, col - r), r2 = min(m - 1, row + r), c2 = min(n - 1, col + r). The answer is P[r2+1][c2+1] - P[r1][c2+1] - P[r2+1][c1] + P[r1][c1]. That's O(m*n) total after O(m*n) setup. The pitfall is the clamping and the inclusion-exclusion signs, plus using the padded index shift incorrectly. Check Example 1: the top-left cell with r = 1 clamps to rows 0-1 and cols 0-1, giving 4. BFS is a red herring here, since a flood outward from each cell repeats work massively. If the indices tangle mid-assessment, StealthCoder is the hedge that reads the problem and hands you the clamped prefix-sum solution.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Matrix Region Sum cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Rippling's OA.

Rippling reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Matrix Region Sum FAQ

What's the trick for Matrix Region Sum?+

Use a 2D prefix sum, then answer each cell's window in constant time. Clamp the window to the matrix bounds with max and min before querying. That handles every border and corner without special cases, and total work stays O(m*n).

Why not use BFS even though it's hinted?+

BFS from each cell visits up to (2r+1)^2 neighbors, which is far too slow at 1000 x 1000 with large r. The window is a rectangle, so a prefix sum is the right tool. BFS only fits if distance, not a rectangle, defined the region.

How do I avoid off-by-one errors?+

Pad the prefix array with a zero row and zero column so P is (m+1) x (n+1). Then query with r2+1 and c2+1 for the high end and r1, c1 for the low end. Test the top-left cell of Example 1, which should give 4.

What if r is bigger than the matrix?+

Clamping covers it. With r = 1000 on a smaller grid, every window clamps to the whole matrix, so every cell gets the full sum. Max and min on the bounds means no extra branch is needed. Example 2 shows this partly with r = 2 on 5 columns.

How do I prepare for this in 48 hours?+

Write the 2D prefix sum from scratch twice, once with padding. Then code the clamped window query and verify against both examples by hand. Also think about the 32-bit sum guarantee, so you know the arithmetic won't overflow in your language.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Rippling.

OA at Rippling?
Invisible during screen share
Get it