Clipped Box Average
Updated
Problem statement
You are given an image image as a rectangular grid of integers. Each cell of the grid is a pixel. You are also given two positive odd numbers, kernelHeight and kernelWidth. Together they set the size of the kernel: a box kernelHeight rows tall and kernelWidth columns wide. For every pixel, you compute the average of the pixels inside the box around it.
Center the box on each pixel [r][c] in turn, with a stride of 1. The box covers rows r - kernelHeight / 2 through r + kernelHeight / 2. It covers columns c - kernelWidth / 2 through c + kernelWidth / 2. Both ends are included. The divisions in these formulas are integer divisions.
- Ignore the parts of the box that fall outside the image. Do not pad with extra values.
- For each pixel, add up the image values inside its box. Divide that sum by the number of pixels inside the box.
- Do not round the averages. Return them as a floating-point grid with the same shape as
image.
The box always contains its center pixel, so the count is never zero. The box may be taller or wider than the image.
Each returned value is accepted when its error is at most 10^-6 * max(1, abs(expected)).
Optimization follow-up
First write a direct solution. Then explain how precomputed rectangle sums bring the total running time down to O(rows * cols). Also explain how you get the pixel count of a box near the border.
Function
clippedBoxAverage(image: int[][], kernelHeight: int, kernelWidth: int) → double[][]Examples
Example 1
image = [[1,2,3],[4,5,6],[7,8,9]]kernelHeight = 3kernelWidth = 3return = [[3.0,3.5,4.0],[4.5,5.0,5.5],[6.0,6.5,7.0]]At the top-left pixel, the box hangs off the top and left edges. Only 1, 2, 4, 5 are inside, so the average is 12 / 4 = 3. At the top-middle pixel, six pixels are inside, so the average is 21 / 6 = 3.5. At the center, all nine pixels are inside, so the average is 45 / 9 = 5. The other pixels work the same way.
Example 2
image = [[2,8,0,6]]kernelHeight = 1kernelWidth = 3return = [[5.0,3.3333333333333335,4.666666666666667,3.0]]The image is a single row. Each box covers a pixel and its left and right neighbors. The four box sums are 10, 10, 14, 6. The boxes hold 2, 3, 3, 2 pixels, because the boxes at both ends hang off one side. Divide each sum by its own count to get the output.
Example 3
image = [[1,-2],[7,10]]kernelHeight = 5kernelWidth = 7return = [[4.0,4.0],[4.0,4.0]]The box is taller and wider than the image, so every box covers the whole image. Its four pixels sum to 16, so every average is 4.
Constraints
1 <= image.length <= 32.- Every image row has the same length, with
1 <= image[0].length <= 32. -10^9 <= image[r][c] <= 10^9.1 <= kernelHeight, kernelWidth <= 63; both dimensions are odd.
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