Vanilla RNN Hidden States
Updated
Problem statement
Implement the forward pass of a vanilla recurrent neural network with a tanh cell over one input sequence. All weights are fixed. Compute the hidden state after every input. Only the forward pass is asked for.
Cell and sequence
inputshasTrows in time order. Each row hasDfeatures.inputWeightshas shapeH x D, andrecurrentWeightshas shapeH x H. Rowrof each matrix produces hidden coordinater.biasandinitialStateeach holdHvalues. Start withprevious = initialState.
For input row t and each hidden coordinate r, compute:
value = bias[r]
+ sum(inputWeights[r][j] * inputs[t][j], j = 0..D-1)
+ sum(recurrentWeights[r][k] * previous[k], k = 0..H-1)
following[r] = tanh(value)
Every coordinate of following must read the same old previous vector. Only after the whole new vector is done does it become previous for the next input.
Returned states
- Return a matrix with
Trows andHcolumns. It holds the hidden state after each input, in time order. - Do not include
initialStateas an output row. An empty input sequence returns[]. - Each output row must keep its own values, so later steps must not overwrite it. Do not change any of the given arrays.
- Use plain loops and standard math functions such as
tanh. Do not use outside tensor or ML libraries. Training, backpropagation, and LSTM gates are not part of this task. - Each returned value is accepted within absolute error
10^-6.
Function
rnnHiddenStates(inputs: double[][], inputWeights: double[][], recurrentWeights: double[][], bias: double[], initialState: double[]) → double[][]Examples
Example 1
inputs = [[0],[1],[-1]]inputWeights = [[1]]recurrentWeights = [[0]]bias = [0]initialState = [0.75]return = [[0.0],[0.7615941559557649],[-0.7615941559557649]]The recurrent weight is 0, so each step depends only on its input. The states are tanh(0), tanh(1), and tanh(-1).
Example 2
inputs = [[0],[0],[0]]inputWeights = [[0]]recurrentWeights = [[1]]bias = [0]initialState = [1]return = [[0.7615941559557649],[0.6420149920119999],[0.5662699759614797]]The input contribution and bias are zero. Starting at 1, each step applies tanh to the previous hidden value. The starting 1 is not an output row.
Example 3
inputs = [[0.5,-1]]inputWeights = [[1,0],[0,1]]recurrentWeights = [[0,0],[0,0]]bias = [0,0]initialState = [0,0]return = [[0.46211715726000974,-0.7615941559557649]]The input weight matrix is the identity, and all recurrent weights are zero. So each hidden value is just tanh of its own input, and the only returned row is [tanh(0.5), tanh(-1)].
Constraints
0 <= T <= 30,1 <= D <= 8, and1 <= H <= 8.- Every array has the shape stated above. When
T = 0, takeDfrom the nonempty rows ofinputWeights. - All values are finite. Entries of
inputs,inputWeights, andbiaslie in[-2, 2]. Entries ofinitialStatelie in[-1, 1]. - For each row
rofrecurrentWeights,sum(abs(recurrentWeights[r][k]), k = 0..H-1) <= 1. This limit keeps rounding errors from growing along the sequence.
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
- Implement Cross-Entropy LossOpenAI · PHONE SCREENOpenAI interview questions
- Scaled Dot-Product AttentionAmazon · PHONE SCREEN
- Transformer Attention and SwiGLU Forward PassGoogle · ONSITE INTERVIEWGoogle interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Logistic Regression DiagnosticsLinkedIn · PHONE SCREENLinkedIn interview questions
- Spiral Matrix TraversalByteDance · PHONE SCREENByteDance interview questions
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN
- Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions