Reported November 2019
Teslasimulation

Batched Strided Convolution

Reported by candidates from Tesla's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Tesla OA. Under 2s to a working solution.
Founder's read

The Tesla OA reported in November 2019 asks you to write a batched, multi-channel, strided 2D convolution with plain loops. No library, no padding, no bias. It looks like a ML question but it's really index bookkeeping. The naive version breaks on the flattened layouts: input planes are b * channels + c, kernels are channel-major, and output planes are b * outputChannels + f. Get one multiplier wrong and every example still looks almost right. If you blank on the indexing live, StealthCoder is the safety net that runs invisibly while you finish.

The problem

Implement a batched, multi-channel two-dimensional convolution using direct loops.
The logical input tensor has shape [batchSize][channels][height][width]. In the callable representation, input flattens the first two dimensions into shape [batchSize * channels][height][width]; plane b * channels + c is channel c of batch item b.
Each row kernels[f] stores output filter f in channel-major order. Its entry for input channel c, kernel row kr, and kernel column kc is at index c * kernelHeight * kernelWidth + kr * kernelWidth + kc.
Use the supplied positive stride, no padding, and neural-network cross-correlation semantics: do not reverse the kernel, add a bias, or apply an activation.
The output height is floor((height - kernelHeight) / stride) + 1, and the output width is defined analogously. Return the output with batch and output-channel dimensions flattened in batch-major order: plane b * outputChannels + f stores filter f for batch item b.

Function
convolveBatched(input: int[][][], batchSize: int, channels: int, kernels: int[][], kernelHeight: int, kernelWidth: int, stride: int) → int[][][]

Examples
Example 1
input = [[[1,2,3],[4,5,6],[7,8,9]]]
batchSize = 1
channels = 1
kernels = [[1,0,0,-1]]
kernelHeight = 2
kernelWidth = 2
stride = 1
return = [[[-4,-4],[-4,-4]]]
There is one batch item and one filter. At the top-left position, the sum is 1 * 1 + 2 * 0 + 4 * 0 + 5 * (-1) = -4.
Example 2
input = [[[1,2,3],[4,5,6],[7,8,9]],[[9,8,7],[6,5,4],[3,2,1]]]
batchSize = 2
channels = 1
kernels = [[1,1,1,1]]
kernelHeight = 2
kernelWidth = 2
stride = 2
return = [[[12]],[[28]]]
The stride leaves one valid window per batch item. Their sums are 12 and 28.
Example 3
input = [[[1,2,3],[4,5,6]],[[10,20,30],[40,50,60]]]
batchSize = 1
channels = 2
kernels = [[1,1,0,0],[0,0,1,-1]]
kernelHeight = 1
kernelWidth = 2
stride = 1
return = [[[3,5],[9,11]],[[-10,-10],[-10,-10]]]
The first filter adds adjacent values from channel 0. The second subtracts adjacent values in channel 1.

Constraints
1 <= batchSize <= 4.
1 <= channels <= 8.
input.length == batchSize * channels.
1 <= height, width <= 20.
1 <= kernels.length <= 8.
1 <= kernelHeight <= height and 1 <= kernelWidth <= width.
Every row of kernels has length channels * kernelHeight * kernelWidth.
1 <= stride <= max(height, width).
All input and kernel values are between -100 and 100, inclusive.
Every output value fits in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is simulation with careful index math. Compute outH = (height - kernelHeight) / stride + 1 using integer division, and outW the same way. Then run six nested loops: batch b, filter f, output row i, output col j, channel c, kernel row kr, kernel col kc. The input value is input[b * channels + c][i * stride + kr][j * stride + kc]. The kernel weight is kernels[f][c * kH * kW + kr * kW + kc]. Write to output[b * numFilters + f][i][j]. The classic pitfall is mixing up channels with output filters when flattening, or using the wrong stride offset so windows run off the edge. Don't flip the kernel, that's cross-correlation. Check Example 3 by hand, since it tests multiple channels with multiple filters. If the indexing slips during the live OA, StealthCoder can supply the loop structure as a hedge.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Batched Strided Convolution cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Tesla's OA.

Tesla reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Batched Strided Convolution FAQ

How hard is the Tesla batched convolution OA question really?+

The algorithm is easy. It's just nested loops and a sum. The difficulty is the flattened indexing across batch, channel, and filter. Expect to spend most of your time on index formulas and checking the examples, not on any clever technique.

What's the trick to the output size?+

Use floor((height - kernelHeight) / stride) + 1 for height and the same for width. Integer division in most languages already floors for these nonnegative values. Constraints guarantee the kernel fits, so the output is at least 1 by 1.

How do I index the flattened kernel?+

For filter f, the weight for channel c, kernel row kr, and kernel column kc sits at c * kernelHeight * kernelWidth + kr * kernelWidth + kc in kernels[f]. Channel-major means all of channel 0 comes first, then channel 1, and so on.

Do I need to flip the kernel or add a bias?+

No. The problem says cross-correlation semantics. Don't reverse the kernel, don't add a bias, and don't apply an activation. Just multiply and sum over channels and the kernel window.

How do I prepare for this in 48 hours?+

Hand-trace Example 3 until you can write the six-loop version without hesitating. Then test a stride larger than 1 and a kernel that equals the full input size. Those two cases catch most off-by-one and bounds mistakes.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Tesla.

OA at Tesla?
Invisible during screen share
Get it