FastPrepMulti-Head Attention Forward Pass
Problems/Multi-Head Attention Forward Pass

Multi-Head Attention Forward Pass

Waymo logoWaymo● HardFULLTIMEPHONE SCREEN

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: From the report: a Waymo phone screen for a full-time machine learning engineer role, from a candidate who was changing jobs. Every task in that round was coded by hand in NumPy. Multi-head attention was the second task, after a numerically stable softmax. The candidate did not finish it: partway through, the interviewer moved the round on to a convolution (CNN) task. The softmax and convolution tasks are separate problems.

Our choices: the report gives no shapes, weights, limits, precision or examples. Self-attention on one sequence, the four given weight matrices, heads taking equal blocks of columns, no mask and no bias, the limits, the tolerance and the examples are ours. The round used NumPy; here you write the same math with plain arrays in any supported language, because NumPy is not available in this editor.

Our version is about 70% the same as the reported question.

Implement the forward computation of a multi-head self-attention layer by hand. The input x is a sequence of n tokens: row i is token i (rows are counted from 0), a vector of width d. The layer has four weight matrices of size d x d: wq, wk and wv make the queries, keys and values, and wo mixes the heads at the end. It splits the width into heads heads, each of width d_k = d / heads. Return the n x d output matrix, computed as follows.

  1. Compute Q = x · wq, K = x · wk and V = x · wv with ordinary matrix products. Each is n x d.
  2. Head h, counting from 0, owns columns h · d_k through (h + 1) · d_k - 1 of Q, K and V.
  3. In each head, token i gets a score for every token t: the dot product of the head's part of Q[i] with the head's part of K[t], divided by sqrt(d_k).
  4. Softmax turns token i's n scores into weights: the weight of token t is exp(score[t]) divided by the sum of exp over all n scores. Token i's result in that head is the weighted sum, over all t, of the head's part of V[t], using those weights.
  5. Join the heads' results column by column in head order, head 0 on the left, to get an n x d matrix, and multiply it by wo.

There is no mask and no bias: every token attends to every token, itself included. Scores here can be far above 709, where exp overflows a double, so your softmax must still return the correct weights, with no overflow and no 0 / 0.

Do all the arithmetic in your own code: no library attention, softmax or matrix routine.

Function

multiHeadAttention(x: double[][], wq: double[][], wk: double[][], wv: double[][], wo: double[][], heads: int) → double[][]

Examples

Example 1

x = [[1.0,1.0],[0.0,2.0]]wq = [[1.0,0.0],[0.0,1.0]]wk = [[1.0,0.0],[0.0,1.0]]wv = [[1.0,0.0],[0.0,1.0]]wo = [[1.0,0.0],[0.0,1.0]]heads = 1return = [[0.5,1.5],[0.1955703174930431,1.804429682506957]]

One head, so d_k = 2 and every weight matrix is the identity: queries, keys and values all equal x. Token 0 scores [2, 2] / sqrt(2) = [1.4142, 1.4142], a tie, so its weights are [0.5, 0.5] and its output is [0.5, 1.5]. Token 1 scores [2, 4] / sqrt(2) = [1.4142, 2.8284]; softmax gives weights [0.1956, 0.8044], so its output is 0.1956 · [1, 1] + 0.8044 · [0, 2] = [0.1956, 1.8044].

Example 2

x = [[1.0,2.0],[3.0,-1.0]]wq = [[1.0,0.0],[0.0,1.0]]wk = [[1.0,0.0],[0.0,1.0]]wv = [[1.0,0.0],[0.0,1.0]]wo = [[0.0,1.0],[1.0,0.0]]heads = 2return = [[1.9925821305300957,2.761594155955765],[-0.8577223804672996,2.9950547536867305]]

Two heads of width d_k = 1, so the scale is 1. Head 0 uses column 0: values [1, 3]; token 0 scores [1, 3], weights [0.1192, 0.8808], result 2.7616; token 1 scores [3, 9], result 2.9951. Head 1 uses column 1: values [2, -1]; token 0 gets 1.9926, token 1 gets -0.8577. Concatenating gives [[2.7616, 1.9926], [2.9951, -0.8577]], and wo swaps the two columns.

Example 3

x = [[5.0,5.0],[-5.0,5.0]]wq = [[5.0,0.0],[0.0,5.0]]wk = [[5.0,0.0],[0.0,5.0]]wv = [[1.0,0.0],[0.0,1.0]]wo = [[1.0,0.0],[0.0,1.0]]heads = 1return = [[5.0,5.0],[-5.0,5.0]]

Each token's score with itself is 1250 / sqrt(2) = 883.88 and with the other token is 0. exp(883.88) overflows a double, so a softmax that does not subtract the row's largest score first breaks. With the shift the weights are [1, 0] and [0, 1], so each token gets its own value back.

Constraints

  • 1 <= x.length <= 12 (the number of tokens, n)
  • 1 <= x[i].length <= 12 (the model width, d), and every row of x has the same length.
  • wq, wk, wv and wo each have d rows and d columns.
  • 1 <= heads <= d, and d is a multiple of heads.
  • Every entry of x, wq, wk, wv and wo is between -5 and 5, with at most two digits after the decimal point.
  • Each returned value is accepted if it is within 10-9 of the expected value, or within 10-9 times the expected value when that value is larger than 1 in size.

More Waymo Phone Screen questions

  • 2D Convolution Layer Forward PassPHONE SCREEN · Seen Dec 2026
  • Numerically Stable SoftmaxPHONE SCREEN · Seen Dec 2026
  • Shortest Travel Time in a Grid with Traffic LightsPHONE SCREEN · Seen Nov 2026
  • Fixed-Order Task Run Time With CooldownPHONE SCREEN · Seen Oct 2026
  • Reorderable Task Scheduler With CooldownPHONE SCREEN · Seen Oct 2026
  • Island Area and Water BoundaryPHONE SCREEN · Seen Sep 2026
  • Vehicle Maintenance Event ReadinessPHONE SCREEN · Seen Sep 2026
  • Obstacle-Aware Return to OriginPHONE SCREEN · Seen Sep 2026
  • Shortest Paths to Multiple TargetsPHONE SCREEN · Seen Sep 2026
  • Deterministic Depth-First Graph TraversalPHONE SCREEN · Seen Sep 2026

Similar questions at other companies

  • Prefix Matrix Products and AutogradOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Choose the Next Minesweeper ActionRunloop · PHONE SCREEN
  • Best-Fit Plane Normal from 3D PointsThe Voleon Group · PHONE SCREEN
  • Town Temperature Regression AnalysisCitadel · PHONE SCREENCitadel interview questions
  • Scaled Dot-Product AttentionAmazon · PHONE SCREEN
  • Implement Multi-Head AttentionApple · PHONE SCREENApple interview questions
  • Multi-Head Attention BlockScale AI · PHONE SCREENScale AI interview questions
  • Range of a Third CorrelationTower Research Capital · PHONE SCREEN
  • Logistic Regression DiagnosticsLinkedIn · PHONE SCREENLinkedIn interview questions
  • Best Starting Cell for Restaurant DistanceDoorDash · PHONE SCREENDoorDash interview questions

Waymo interview experiences

  • Waymo New Grad MLE Phone Screen: Traffic Lights: 1 Experience
All Waymo interview questionsSee Waymo hiring insights
Saved
class Solution {
    public double[][] multiHeadAttention(double[][] x, double[][] wq, double[][] wk, double[][] wv, double[][] wo, int heads) {
        // Write your code here.
    }
}
Checking account…