Square-Neighborhood Image Blur
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure behind this ZipRecruiter question, reported in September 2024, is a 2D prefix sum grid. The task is a square-neighborhood image blur, and the brute force looks fine until you see a 500 by 500 matrix with radius up to 500. That's the trap. If you have this OA in a day or two, learn the prefix-sum rectangle trick and the neighbor-exclusion detail. StealthCoder is the safety net on the live assessment if your mind goes blank mid-problem, but the pattern is small enough to hold in your head.
The problem
For each grayscale pixel, consider all other in-bounds cells within row and column distance radius. Floor the neighbor mean, then floor the average of that mean and the original pixel. Preserve a pixel with no neighbors. Return a new matrix using only original-image values for every calculation. Function blurImage(image: int[][], radius: int) → int[][] Examples Example 1 image = [[9,6],[3,0]] radius = 1 return = [[6,5],[4,3]] Each pixel sees the other three cells. Example 2 image = [[7]] radius = 0 return = [[7]] The only pixel has no neighbor. Constraints 1 <= rows,columns <= 500 0 <= pixel <= 255 0 <= radius <= 500
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a 2D prefix sum over the original image. For each cell, clamp the window to r-radius..r+radius and c-radius..c+radius inside the grid. Get the window sum in O(1) with inclusion-exclusion, then subtract the pixel itself to get the neighbor sum. The neighbor count is window area minus one. If the count is zero, keep the original pixel. Otherwise compute floor(neighborSum / count), then floor((mean + original) / 2). Pitfalls: writing results back into the input, which breaks the original-values rule, off-by-one errors in the padded prefix array, and forgetting that the center is excluded. Brute force is O(n*m*radius^2) and will time out. The prefix approach is O(n*m). If you freeze on the index math during the live OA, StealthCoder can supply the clamped-window formula while you keep control of the edge cases.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Square-Neighborhood Image Blur 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Square-Neighborhood Image Blur FAQ
What's the trick in the ZipRecruiter image blur problem?+
Use a 2D prefix sum so every square window sum costs O(1). Clamp the window to the grid, subtract the center pixel, and divide by the neighbor count. Without it, large radius values make the brute force far too slow.
How hard is this problem really?+
Medium. The idea is standard, but the details bite: excluding the center, flooring twice, and handling pixels with no neighbors. If you've seen a range-sum-query matrix problem, you already know the core.
Do I modify the image in place?+
No. The problem says to use only original-image values for every calculation. Allocate a new output matrix and build the prefix sums from the untouched input so earlier results never leak into later cells.
What edge cases should I test?+
Test a 1x1 image with radius 0, which returns the pixel unchanged. Test a radius larger than the grid, where the window clamps to the whole image. Test corners and single-row or single-column grids. Check the floor behavior with small values like 0 and 1.
How do I prepare in 48 hours?+
Write the 2D prefix sum and the inclusion-exclusion query from memory twice. Then solve this blur with clamped bounds. Practice the integer floor math, since all values are nonnegative and normal integer division works. That covers nearly everything this problem tests.