Reported July 2025
Googleprefix sum

Inclusive Rectangle Coverage Counts

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

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

The mistake that sinks a first attempt on this Google OA, reported in July 2025, is painting every rectangle cell by cell. With up to 100000 rectangles on a 500 x 500 grid, that's billions of writes and a timeout. This is a 2D difference array problem, and it's a prefix-sum pattern in disguise. You mark four corners per rectangle, then sweep once. If you blank on the corner offsets when the clock's running, StealthCoder sits invisibly on your screen as a safety net and hands you the solution. Know the trick first and you won't need it.

The problem

You are given an integer n and an array rectangles. Start with an n x n matrix of zeros.
Each rectangle is represented as [top, left, bottom, right] using zero-based row and column indices. Its boundaries are inclusive. For every rectangle, add 1 to every matrix cell whose row is between top and bottom and whose column is between left and right.
Return the completed matrix of coverage counts.

Function
countRectangleCoverage(n: int, rectangles: int[][]) → int[][]

Examples
Example 1
n = 3
rectangles = [[0,0,1,1],[1,1,2,2]]
return = [[1,1,0],[1,2,1],[0,1,1]]
The two rectangles overlap only at (1,1), so that cell has count 2. Cells covered by one rectangle have count 1.
Example 2
n = 4
rectangles = [[0,1,3,2],[1,0,2,3],[2,2,2,2]]
return = [[0,1,1,0],[1,2,2,1],[1,2,3,1],[0,1,1,0]]
The vertical and horizontal rectangles overlap across rows 1 and 2, columns 1 and 2. The one-cell rectangle raises (2,2) to 3.
Example 3
n = 2
rectangles = []
return = [[0,0],[0,0]]
With no rectangles, every cell keeps its initial coverage count of 0.

Constraints
1 <= n <= 500
0 <= rectangles.length <= 100000
rectangles[i].length == 4
For every rectangle, 0 <= top <= bottom < n and 0 <= left <= right < n.
Rectangle boundaries are inclusive.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a 2D difference array. Make a (n+1) x (n+1) grid. For each rectangle [top, left, bottom, right], do diff[top][left] += 1, diff[top][right+1] -= 1, diff[bottom+1][left] -= 1, diff[bottom+1][right+1] += 1. Then take a 2D prefix sum over the grid, and the result for each cell is its coverage count. Total work is O(rectangles + n^2), which is tiny. The common pitfalls are off-by-one errors on the inclusive boundaries and forgetting the extra row and column, which causes index errors when bottom or right equals n-1. Also watch the empty rectangles case, which should return all zeros. Return only the top-left n x n slice. If the corner signs slip your mind mid-OA, StealthCoder is the hedge that gets you unstuck fast, but drawing the 2x2 corner case on paper takes thirty seconds.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Inclusive Rectangle Coverage Counts 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Inclusive Rectangle Coverage Counts FAQ

What's the trick for this Google OA problem?+

Use a 2D difference array. Add 1 at the top-left corner, subtract 1 just past the right edge and just past the bottom edge, then add 1 at the bottom-right diagonal. A single 2D prefix sum afterward gives every cell's coverage count in one pass.

Why does brute force fail here?+

Each rectangle can cover up to 250000 cells and there can be 100000 rectangles. That's tens of billions of updates in the worst case. The difference array makes each rectangle a constant-time update of four cells, then one n^2 sweep finishes the job.

How do I handle the inclusive boundaries?+

Inclusive means the subtraction happens at right+1 and bottom+1, not at right and bottom. Allocate an (n+1) x (n+1) grid so those indices exist even when a rectangle touches the last row or column. Trim back to n x n at the end.

Is this prefix-sum pattern still asked in 2025?+

Yes. Range-update problems on arrays and grids keep showing up, and this one was reported for Google in July 2025. Learn the 1D difference array first, then the 2D version is just the same idea applied along both axes.

How do I prepare for this in 48 hours?+

Write the 1D difference array from memory, then extend it to 2D. Test on example 1 by hand, and test the empty list and a full-grid rectangle. Practice the prefix sum step: cell = diff + up + left - upleft. That covers almost every variant.

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

OA at Google?
Invisible during screen share
Get it