Valid Max Pooling
Updated
Problem statement
You are given one feature map image: a rectangular grid of integers. Compute the forward pass of max pooling on it: slide a window over the grid and keep the largest value in each window. The window has kernelHeight rows and kernelWidth columns. It moves down by strideHeight rows and right by strideWidth columns at a time.
- The first window starts at the top-left cell,
[0,0]. - The next windows start at rows
strideHeight, 2 * strideHeight, ...and at columnsstrideWidth, 2 * strideWidth, .... - Use only windows that fit completely inside the grid. There is no padding and no dilation. Skip any window at the end that would stick out past the edge.
- For each full window, take the largest of all the values it covers. Negative values and repeated values count like any other value.
Return the results as a grid in the same row and column order: output[r][c] is the maximum of the window that starts at [r * strideHeight, c * strideWidth]. The output has 1 + floor((image.length - kernelHeight) / strideHeight) rows and 1 + floor((image[0].length - kernelWidth) / strideWidth) columns.
Every input is valid, and the window always fits in both directions. A stride may be larger than the window, or even larger than the grid. Return only the maximum values. You do not need gradients, the positions of the maxima, or batch and channel axes. Do not use a pooling library.
Function
maxPool2D(image: int[][], kernelHeight: int, kernelWidth: int, strideHeight: int, strideWidth: int) → int[][]Examples
Example 1
image = [[1,3,2,0],[4,6,5,1],[7,2,9,8]]kernelHeight = 2kernelWidth = 2strideHeight = 1strideWidth = 2return = [[6,5],[7,9]]Windows start at rows 0, 1 and columns 0, 2. That gives four windows, and their largest values are 6, 5, 7, 9, read row by row. Laid out in that row and column order, they form the answer.
Example 2
image = [[-4,-8,-3,-9,-2],[-6,-7,-5,-1,-10],[-11,-12,-13,-14,-15]]kernelHeight = 2kernelWidth = 3strideHeight = 2strideWidth = 2return = [[-3,-1]]Only two windows fit, starting at [0,0] and [0,2]. Their largest values are -3 and -1. A window starting at row 2 would stick out past the bottom, so the last row is not used by any window.
Example 3
image = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]kernelHeight = 1kernelWidth = 1strideHeight = 2strideWidth = 3return = [[1,4],[9,12]]The window is a single cell, so each result is just that cell's value. Windows start at rows 0, 2 and columns 0, 3, and the cells in between are skipped. These four windows are the four corners of the grid.
Constraints
1 <= image.length, image[0].length <= 48.- All image rows have the same nonzero length.
-10^9 <= image[r][c] <= 10^9.1 <= kernelHeight <= image.length.1 <= kernelWidth <= image[0].length.1 <= strideHeight, strideWidth <= 100.
More NVIDIA Onsite Interview questions
- Check Endianness Of A Byte ArrayONSITE INTERVIEW · Seen Sep 2026
- Implement MallocONSITE INTERVIEW · Seen Sep 2026
- Implement PrintfONSITE INTERVIEW · Seen Sep 2026
- Merge Two Arrays Into Sorted OrderONSITE INTERVIEW · Seen Sep 2026
- Middle Node Of A Linked ListONSITE INTERVIEW · Seen Sep 2026
- Ping Reply TrackingONSITE INTERVIEW · Seen Sep 2026
- Reverse A StringONSITE INTERVIEW · Seen Sep 2026
- Maximum Fish in a GridONSITE INTERVIEW · Seen Sep 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- First Register Overflow TickONSITE INTERVIEW · Seen May 2024
Similar questions at other companies
- Sliding Window Maximum in a MatrixCitadel · OACitadel interview questions
- Maximum Earnings Across WarehousesAmazon · ONSITE INTERVIEW
- Rolling Hit CounterDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
- Longest Subarray Within an Absolute-Difference LimitGoogle · ONSITE INTERVIEWGoogle interview questions
- Rolling CSAT TrackerDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions
- Top-Scoring Items in a Time WindowPinterest · ONSITE INTERVIEWPinterest interview questions
- Longest Continuous Subarray Within an Absolute-Difference LimitUber · ONSITE INTERVIEWUber interview questions
- Rate Limiter with Global and Per-User LimitsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Sliding-Window Rate LimiterSnowflake · ONSITE INTERVIEWSnowflake interview questions
- Rate Limiter Sliding Window With Per-Entity LimitsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions