PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Tesla

Implement 2D convolution forward pass

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of 2D convolution mechanics and practical low-level tensor manipulation, including kernel shape handling, padding and stride effects, multi-channel input/output interactions, and optional bias integration.

  • easy
  • Tesla
  • Coding & Algorithms
  • Machine Learning Engineer

Implement 2D convolution forward pass

Company: Tesla

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

## Problem Implement the **forward pass of a 2D convolution (conv2d)** from scratch (no deep learning libraries). You are given: - Input tensor `x` with shape **(N, C_in, H, W)** (NCHW layout) - Filter weights `w` with shape **(C_out, C_in, K_h, K_w)** - Optional bias `b` with shape **(C_out,)** (may be `None`) - Integer stride `s_h, s_w` - Integer padding `p_h, p_w` (zero-padding applied to height/width) Compute the output tensor `y` with shape **(N, C_out, H_out, W_out)** where: \[ H_{out} = \left\lfloor \frac{H + 2p_h - K_h}{s_h} \right\rfloor + 1, \quad W_{out} = \left\lfloor \frac{W + 2p_w - K_w}{s_w} \right\rfloor + 1 \] For each output element: \[ y[n, c_{out}, i, j] = \sum_{c_{in}=0}^{C_{in}-1} \sum_{u=0}^{K_h-1} \sum_{v=0}^{K_w-1} \; x_{pad}[n, c_{in}, i\cdot s_h + u, j\cdot s_w + v] \cdot w[c_{out}, c_{in}, u, v] + b[c_{out}] \] where `x_pad` is `x` padded with zeros by `(p_h, p_w)`. ## Requirements - Return `y` as a dense numeric array/tensor. - Do not use existing convolution operators. - Handle edge cases such as `b is None`, non-square kernels, and different strides/padding. ## Constraints (typical for an interview unit test) - Sizes are small enough that an \(O(N\cdot C_{out}\cdot C_{in}\cdot H_{out}\cdot W_{out}\cdot K_h\cdot K_w)\) implementation passes. - Inputs are floating point numbers. ## Example (shape check) If `x` is `(1, 3, 32, 32)`, `w` is `(8, 3, 3, 3)`, stride `(1,1)`, padding `(1,1)`, then output shape is `(1, 8, 32, 32)`.

Quick Answer: This question evaluates understanding of 2D convolution mechanics and practical low-level tensor manipulation, including kernel shape handling, padding and stride effects, multi-channel input/output interactions, and optional bias integration.

Related Interview Questions

  • Write SQL Data Transformation Queries - Tesla (medium)
  • Implement a Rollback Key-Value Store - Tesla (hard)
  • Compute suffix sums over waypoints - Tesla (hard)
  • Compute time to burn tree - Tesla (medium)
  • Coordinate workers across two exclusive targets - Tesla (hard)
Tesla logo
Tesla
Dec 15, 2025, 12:00 AM
Machine Learning Engineer
Technical Screen
Coding & Algorithms
15
0

Problem

Implement the forward pass of a 2D convolution (conv2d) from scratch (no deep learning libraries).

You are given:

  • Input tensor x with shape (N, C_in, H, W) (NCHW layout)
  • Filter weights w with shape (C_out, C_in, K_h, K_w)
  • Optional bias b with shape (C_out,) (may be None )
  • Integer stride s_h, s_w
  • Integer padding p_h, p_w (zero-padding applied to height/width)

Compute the output tensor y with shape (N, C_out, H_out, W_out) where:

Hout=⌊H+2ph−Khsh⌋+1,Wout=⌊W+2pw−Kwsw⌋+1H_{out} = \left\lfloor \frac{H + 2p_h - K_h}{s_h} \right\rfloor + 1, \quad W_{out} = \left\lfloor \frac{W + 2p_w - K_w}{s_w} \right\rfloor + 1Hout​=⌊sh​H+2ph​−Kh​​⌋+1,Wout​=⌊sw​W+2pw​−Kw​​⌋+1

For each output element:

y[n,cout,i,j]=∑cin=0Cin−1∑u=0Kh−1∑v=0Kw−1  xpad[n,cin,i⋅sh+u,j⋅sw+v]⋅w[cout,cin,u,v]+b[cout]y[n, c_{out}, i, j] = \sum_{c_{in}=0}^{C_{in}-1} \sum_{u=0}^{K_h-1} \sum_{v=0}^{K_w-1} \; x_{pad}[n, c_{in}, i\cdot s_h + u, j\cdot s_w + v] \cdot w[c_{out}, c_{in}, u, v] + b[c_{out}]y[n,cout​,i,j]=∑cin​=0Cin​−1​∑u=0Kh​−1​∑v=0Kw​−1​xpad​[n,cin​,i⋅sh​+u,j⋅sw​+v]⋅w[cout​,cin​,u,v]+b[cout​]

where x_pad is x padded with zeros by (p_h, p_w).

Requirements

  • Return y as a dense numeric array/tensor.
  • Do not use existing convolution operators.
  • Handle edge cases such as b is None , non-square kernels, and different strides/padding.

Constraints (typical for an interview unit test)

  • Sizes are small enough that an O(N⋅Cout⋅Cin⋅Hout⋅Wout⋅Kh⋅Kw)O(N\cdot C_{out}\cdot C_{in}\cdot H_{out}\cdot W_{out}\cdot K_h\cdot K_w)O(N⋅Cout​⋅Cin​⋅Hout​⋅Wout​⋅Kh​⋅Kw​) implementation passes.
  • Inputs are floating point numbers.

Example (shape check)

If x is (1, 3, 32, 32), w is (8, 3, 3, 3), stride (1,1), padding (1,1), then output shape is (1, 8, 32, 32).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Tesla•More Machine Learning Engineer•Tesla Machine Learning Engineer•Tesla Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.