How would you implement a simple box blur on a grayscale image?

Spatial convolution and image filtering basics.
Iterate interior pixels, sum the N by N neighborhood, divide by kernel area, write to a new buffer.
In place updates that blur already blurred values.
What's really being asked
This question checks whether you can move from a mathematical description of a filter to a working implementation. The interviewer cares about your understanding of discrete convolution in the spatial domain, memory access patterns, and boundary handling. It is not about knowing OpenCV or PIL; it is about nested loops, accumulator logic, and the difference between reading from source and writing to destination.
The full answer
First, state that you need two buffers: a source grayscale image and a destination image of identical width and height. Second, describe the iteration: for each pixel that is not on the border, define a neighborhood around it. For a 3 by 3 box blur, that means looking at the pixel itself plus its eight neighbors. Third, explain the accumulation: sum all intensity values in that neighborhood, then divide by the number of elements in the kernel, which is nine for a 3 by 3 kernel. Fourth, write the averaged value into the corresponding location in the destination buffer. Fifth, mention border handling explicitly, such as skipping edge pixels, clamping, or mirroring, so you never read outside the array bounds.
The mistakes people make
A major red flag is proposing an in place update where you overwrite the source image while iterating. Doing so causes already blurred values to leak into the next neighborhood, turning the box blur into an unintended recursive filter. Another red flag is confusing the kernel with the image itself, for example by sliding the kernel matrix over the image and multiplying arbitrary weights instead of performing a uniform average. A third mistake is forgetting integer division or overflow issues when working with uint8 data; a strong candidate notes that the accumulator should use a larger type such as an integer or float before dividing and casting back.
Likely follow ups
The interviewer may ask how to optimize the naive approach. You should mention the separable property of the box blur, which lets you perform two one dimensional passes instead of one two dimensional pass, reducing complexity from order of N squared times kernel area to order of N squared times kernel radius. They may also ask about the sliding window or summed area table techniques that let you update the neighborhood sum in constant time. Another follow up is how the filter behaves at edges and whether zero padding introduces dark borders.
A concrete example
Suppose the source image is 5 pixels wide by 5 pixels tall and you use a 3 by 3 kernel. The center pixel at row 2 column 2 has neighbors from rows 1 through 3 and columns 1 through 3. You add those nine grayscale values, divide by 9, and place the result at destination row 2 column 2. Pixels in row 0, row 4, column 0, and column 4 are left unprocessed or handled with a chosen border strategy. The entire operation is linear and shift invariant, meaning the same arithmetic applies uniformly across the whole image.
Interview question
When implementing a box blur, why is it important to write results into a separate destination buffer rather than updating the source image in place?
- a.It lets the algorithm handle borders without explicit boundary checks
- b.It ensures intermediate sums can be stored in a larger data type to avoid overflow
- c.It prevents already-blurred pixel values from being reused in later neighborhood averagesCorrect
- d.It reduces memory usage compared to allocating a second image array
Why? this is the answer
Using a separate destination buffer guarantees that every neighborhood average reads only original pixel values, not values that have already been blurred and would distort subsequent averages. The overflow issue in option B is addressed by using a larger type for the accumulator during the sum, not by allocating a second image buffer.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
- #computer vision
- #image processing
- #convolution
- #spatial filtering
- #algorithms
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on computer vision — each one lists the topics its interview covers.
See open roles