2D Convolution Layer Forward Pass
Updated
Problem statement
Write the forward pass of a single 2-D convolution layer, the building block of a convolutional neural network (CNN), applied to one image.
The image input has inChannels = input.length channels, each a height x width grid of integers: input[c][r][q] is the value in row r, column q of channel c. The layer has outChannels = bias.length output channels. Output channel o owns one bias value, bias[o], and one kernelHeight x kernelWidth kernel for each input channel.
All outChannels * inChannels kernels sit in one list, weights, grouped by output channel: the kernel that output channel o applies to input channel c is weights[o * inChannels + c]. For example, with 2 input channels and 3 output channels, weights holds 6 kernels. weights[0] and weights[1] belong to output channel 0 (for input channels 0 and 1), weights[2] and weights[3] to output channel 1, and weights[4] and weights[5] to output channel 2. So output channel 2 reads input channel 1 through weights[2 * 2 + 1], which is weights[5].
Each kernel slides over its input channel one cell at a time and is placed only where it lies completely inside the grid; nothing is added around the border. The kernel is used as given, not flipped. The value of output channel o at row i, column j is bias[o] plus, for every input channel c, kernel row u and kernel column v, the product input[c][i + u][j + v] * weights[o * inChannels + c][u][v].
Return the result as outChannels grids, each with height - kernelHeight + 1 rows and width - kernelWidth + 1 columns. Adding the bias is the last step: no ReLU or other activation follows it.
Write the loops yourself; do not call a library convolution routine.
Function
conv2dForward(input: int[][][], weights: int[][][], bias: int[]) → int[][][]Examples
Example 1
input = [[[4,1,0],[2,3,5],[1,0,2]]]weights = [[[1,2],[0,-1]]]bias = [1]return = [[[4,-3],[9,12]]]One input channel and one output channel, so weights holds a single 2 x 2 kernel. The kernel first covers rows 0-1 and columns 0-1 of the input, [[4,1],[2,3]]: 4*1 + 1*2 + 2*0 + 3*(-1) = 3, and the bias 1 makes it 4. Sliding one column right gives -3, and the next row down gives 9 and 12. A 3 x 3 input and a 2 x 2 kernel leave a 2 x 2 output.
Example 2
input = [[[1,0,2,1],[3,1,0,2],[0,2,1,1]],[[2,1,0,3],[1,0,1,2],[2,2,0,1]]]weights = [[[1,0],[0,1]],[[0,2],[1,0]],[[1,1],[0,0]],[[0,0],[-1,2]]]bias = [0,10]return = [[[5,0,11],[7,6,5]],[[10,14,16],[16,9,14]]]With 2 input channels and 2 output channels, weights has 2 * 2 = 4 kernels, and output channel o uses weights[o * 2 + c] on input channel c. So output channel 0 uses weights[0] and weights[1], and output channel 1 uses weights[2] and weights[3]. For output channel 0 at row 0, column 0: input channel 0 gives 1 + 1 = 2, input channel 1 gives 1*2 + 1*1 = 3, and the bias is 0, so the value is 5. Every value of output channel 1 includes its bias 10.
Example 3
input = [[[3,-1],[2,4]],[[0,5],[-2,1]]]weights = [[[1,1],[1,1]],[[1,1],[1,1]],[[2,0],[0,0]],[[0,0],[0,-3]]]bias = [-5,7]return = [[[7]],[[10]]]Each kernel has the same size as the input, so it fits in one place only and every output channel is 1 x 1. Output channel 0 multiplies every input value by 1: (3 - 1 + 2 + 4) + (0 + 5 - 2 + 1) = 12, and the bias -5 gives 7. Output channel 1 keeps only two products, 2*3 from input channel 0 and (-3)*1 from input channel 1: 6 - 3 + 7 = 10.
Constraints
1 <= input.length <= 6, the number of input channelsinChannels.1 <= input[c].length <= 16and1 <= input[c][r].length <= 16, and every input channel has the sameheightandwidth.1 <= bias.length <= 6, the number of output channelsoutChannels.weights.length == outChannels * inChannels.- Every kernel has the same size
kernelHeight x kernelWidth, with1 <= kernelHeight <= heightand1 <= kernelWidth <= width. -100 <= input[c][r][q] <= 100and-100 <= weights[k][u][v] <= 100.-104 <= bias[o] <= 104.- Every output value, and every partial sum on the way to it, lies between
-15,370,000and15,370,000, so a signed 32-bit integer never overflows.
More Waymo Phone Screen questions
- Multi-Head Attention Forward PassPHONE SCREEN · Seen Dec 2026
- Numerically Stable SoftmaxPHONE SCREEN · Seen Dec 2026
- Shortest Travel Time in a Grid with Traffic LightsPHONE SCREEN · Seen Nov 2026
- Fixed-Order Task Run Time With CooldownPHONE SCREEN · Seen Oct 2026
- Reorderable Task Scheduler With CooldownPHONE SCREEN · Seen Oct 2026
- Island Area and Water BoundaryPHONE SCREEN · Seen Sep 2026
- Vehicle Maintenance Event ReadinessPHONE SCREEN · Seen Sep 2026
- Obstacle-Aware Return to OriginPHONE SCREEN · Seen Sep 2026
- Shortest Paths to Multiple TargetsPHONE SCREEN · Seen Sep 2026
- Deterministic Depth-First Graph TraversalPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Spiral Matrix TraversalByteDance · PHONE SCREENByteDance interview questions
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN
- Longest Consecutive Same-Color LineGoogle · PHONE SCREENGoogle interview questions
- Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions
- Diagonal TraverseHive · PHONE SCREENHive interview questions
- Game of Life on a Finite and an Infinite BoardCitadel · PHONE SCREENCitadel interview questions
- Build a Bird’s-Eye-View Height GridNVIDIA · PHONE SCREENNVIDIA interview questions