Tensor Shapes Through a Neural Network
Updated
Problem statement
You are given the shape of an input tensor and a list of neural-network layers. Return the tensor shape before the first layer and after every layer. You only work out sizes. No tensor values or weights are given.
The layers run one after another. The starting shape initialShape is [N, C, H, W]: the batch size, the number of channels, the height and the width. The arrays layerTypes and layerParameters describe the same layers: entry i of each array is layer i, and the layers run in array order.
Layer rules
CONVtakes the parameters[outChannels, kernelHeight, kernelWidth, strideHeight, strideWidth, paddingHeight, paddingWidth]. It turns[N,C,H,W]into[N,outChannels,outputHeight,outputWidth]. Each axis gets the same padding on both sides. The dilation is1.POOLtakes the parameters[kernelHeight, kernelWidth, strideHeight, strideWidth]. It turns a four-dimensional shape into another four-dimensional shape and keepsNandCthe same. Its padding is0and its dilation is1.CONVandPOOLare the spatial layers. For both, the output length of an axis is1 + floor((inputLength + 2 * padding - kernelLength) / stride). Only full windows count. A partial window at the end is dropped.FLATTENtakes no parameters,[]. It turns[N,C,H,W]into[N,C * H * W].LINEARtakes the parameters[outFeatures]. It turns[N,features]into[N,outFeatures].
Every input is a valid model. It has zero or more spatial layers, then maybe one FLATTEN, then zero or more LINEAR layers. The model may stop after any of these parts. You do not need to handle invalid shapes.
If there are L layers, return L + 1 rows. Row 0 is initialShape, and row i + 1 is the output of layer i. So row i is the input of layer i, and row i + 1 is its output. With no layers, return just the initial shape. A row has four numbers before flattening and two numbers after it.
Function
tensorShapeTrace(initialShape: int[], layerTypes: String[], layerParameters: int[][]) → int[][]Examples
Example 1
initialShape = [2,3,8,9]layerTypes = ["CONV","POOL","FLATTEN","LINEAR"]layerParameters = [[4,3,3,2,2,1,1],[2,2,2,2],[],[5]]return = [[2,3,8,9],[2,4,4,5],[2,4,2,2],[2,16],[2,5]]The convolution produces 4 channels and spatial dimensions 4 by 5. Pooling keeps the batch and channels, producing 2 by 2. Flattening gives 4 * 2 * 2 = 16 features per sample, and the linear layer produces 5 features.
Example 2
initialShape = [1,2,10,13]layerTypes = ["CONV","CONV"]layerParameters = [[5,3,4,3,4,0,0],[2,2,1,2,2,1,0]]return = [[1,2,10,13],[1,5,3,3],[1,2,2,2]]The first layer uses complete windows, so the height and width are both 3. The second layer receives that output, uses height padding 1 and width padding 0, and produces shape [1,2,2,2].
Example 3
initialShape = [3,2,2,4]layerTypes = ["FLATTEN","LINEAR","LINEAR"]layerParameters = [[],[7],[2]]return = [[3,2,2,4],[3,16],[3,7],[3,2]]Flattening combines only the channel and spatial axes: 2 * 2 * 4 = 16. Each linear layer replaces the feature count while preserving batch size 3. The output includes the initial shape and all three layer outputs.
Constraints
initialShape.length = 4, and0 <= layerTypes.length = layerParameters.length <= 80.1 <= N, C, outChannels <= 64.- Every spatial input and output dimension is between
1and128. - Every kernel size and stride is between
1and128; convolution padding is between0and64. - Each kernel fits within the current padded input axis, so both spatial outputs are positive.
1 <= outFeatures <= 1048576. Flattening produces at most64 * 128 * 128 = 1048576features.- Layer names, parameter counts and layer ordering follow the stated rules.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Parking Fee CalculatorBreeze · PHONE SCREEN
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Three Points in a Semicircle: Monte Carlo EstimateSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Exponential Backoff DelaysWonderful · PHONE SCREEN
- Add Two Integers Given as StringsJane Street · PHONE SCREENJane Street interview questions
- Fizz and Buzz Words From 1 to nDocuSign · PHONE SCREENDocuSign interview questions
- Recover Document Hierarchy from IndentationReducto · PHONE SCREEN