FastPrepImplement Multi-Head Attention
Problems/Implement Multi-Head Attention

Implement Multi-Head Attention

Google logoGoogle● HardNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Google AI/ML onsite report says the last fifteen minutes of an ML round asked the candidate to hand-code multi-head attention. The report gives no tensor shapes, projection/mask policy, numeric format, signature or examples. This practice uses projection-free Q/K/V input matrices, scaled dot-product attention, stable softmax and fixed-decimal output as disclosed runnable assumptions. Approximate source match: 55%.

Write the forward pass of multi-head scaled dot-product attention, using the input matrices as they are.

You get three integer matrices, query, key and value, each given row by row. Every row in all three has the same width d. The key and value matrices have the same number of rows. You also get heads, which divides d evenly.

Cut every row into heads equal pieces of side-by-side columns, one piece per head. Then, for each query row and each head:

  1. Take the dot product of the query row's piece with the same piece of every key row.
  2. Divide each of these scores by sqrt(d / heads).
  3. Apply softmax across the keys to turn the scores into weights. Use a numerically stable softmax, which subtracts the largest score before taking exponents.
  4. Add up the matching pieces of the value rows, each multiplied by its key's weight.

Join the head results in head order to get the output for that query row.

Return one string per query row. The string lists the row's numbers separated by commas, each written with exactly six decimal places. This problem has no learned projections, masks, biases, dropout or backward pass.

Function

multiHeadAttention(query: int[][], key: int[][], value: int[][], heads: int) → String[]

Examples

Example 1

query = [[1]]key = [[0],[1]]value = [[10],[20]]heads = 1return = ["17.310586"]

The query row is compared with the two key rows, giving scores 0 and 1. With one head of width one, dividing by the square root changes nothing. Softmax gives the second key the bigger weight, so the output mixes the two value rows and lands closer to the second value. That weighted mix, written with six decimal places, is the answer.

Example 2

query = [[1,0]]key = [[1,0],[0,1]]value = [[4,8],[6,2]]heads = 2return = ["4.537883,5.000000"]

Each row has two columns and there are two heads, so each head looks at one column. In the first head, the query matches the first key better, so the first value row gets the bigger weight and the result lands closer to its entry. In the second head, the query scores both keys the same, so the weights are equal and the result is the average of the two value entries. The two head results, joined with a comma, give the answer.

Constraints

  • 1 <= query rows, key rows, d <= 40.
  • Every row has width d; key and value have the same number of rows.
  • 1 <= heads <= d, and heads divides d.
  • Every entry is an integer in [-100, 100].

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Multi-Head Attention Forward PassMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Sparse Plant Infection SimulationOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Implement Multi-Head AttentionByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
  • Count Islands from Streaming RowsAmazon · ONSITE INTERVIEW
  • Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
  • Cats, Rabbits, and Snails Board GameAsana · ONSITE INTERVIEW
  • Self-Attention and Causal Masked Self-AttentionMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Spiral MatrixAthenahealth · ONSITE INTERVIEW
  • Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions

Interview reports that mention this question

  • Google PhD NLP New Grad Onsite: 1 Experience

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public String[] multiHeadAttention(int[][] query, int[][] key, int[][] value, int heads) {
        // Write your code here.
    }
}
Checking account…