Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Convolutional Neural Network in PyTorch
00:00
5 left

Convolutional Neural Network in PyTorch

HardPython

Problem

Implement the inference path of a small convolutional neural network used to classify frames from a Bosch AUTODOME IP starlight 7000i camera. Do not use PyTorch convolution or pooling helpers. Implement the tensor operations directly with Python lists so that padding, stride, channel aggregation, activation, and pooling behavior are explicit.

The network has one convolutional layer, ReLU activation, non-overlapping max pooling, flattening, and a fully connected output layer. Return the index of the class with the largest logit. If logits tie, return the smallest class index.

Formal Specification

Implement cnn_predict(image, kernels, biases, dense_weights, dense_bias, stride, padding, pool_size).

  • image is a 3D list with shape [input_channels][height][width].
  • kernels is a 4D list with shape [output_channels][input_channels][kernel_size][kernel_size].
  • biases contains one value per output channel.
  • dense_weights has shape [classes][flattened_features].
  • dense_bias contains one value per class.
  • Convolution uses zero padding and the specified stride.
  • Apply ReLU before max pooling.
  • Pooling uses square windows of size pool_size and stride pool_size.
  • Return an integer class index.

Constraints

  • 1 <= input_channels, output_channels, classes <= 8
  • 1 <= height, width <= 32
  • 1 <= kernel_size <= 7 and kernel_size is odd
  • 0 <= padding <= kernel_size // 2
  • 1 <= stride <= 3
  • The convolution output dimensions are divisible by pool_size
  • All tensor shapes are valid and consistent

Function Signature

def cnn_predict(image, kernels, biases, dense_weights, dense_bias, stride, padding, pool_size):
Interviewer

Your question is Convolutional Neural Network in PyTorch. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.