Implement Multi-Head Attention
Updated
Problem statement
Implement a projection-free scaled dot-product attention forward pass. query, key and value are row-major integer matrices with the same feature width d; key and value have the same number of rows. heads divides d.
Split each row into contiguous head slices. For each query row and head:
- Compute its dot product with every key slice.
- Divide the scores by
sqrt(d / heads). - Apply a numerically stable softmax across the keys.
- Use those probabilities to combine the matching value slices.
Concatenate the head outputs in head order.
Return one comma-separated string per query row, with each coordinate formatted to exactly six decimal places. There are no learned projections, masks, biases, dropout or backward pass in this problem.
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"]Softmax over scores 0 and 1 weights the second value more heavily.
Example 2
query = [[1,0]]key = [[1,0],[0,1]]value = [[4,8],[6,2]]heads = 2return = ["4.537883,5.000000"]Each one-dimensional head computes its own attention distribution.
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, andheadsdividesd.- Every entry is an integer in
[-100, 100].
More ByteDance Phone Screen questions
- Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
- Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
- Majority ElementPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Nov 2026
- Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Number of Events That Can Be AttendedPHONE SCREEN · Seen Nov 2026
- Meeting RoomsPHONE SCREEN · Seen Oct 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
- Count Islands from Streaming RowsAmazon · ONSITE INTERVIEW
- Zigzag Board Adjacent SwapsUber · PHONE SCREENUber interview questions
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Cats, Rabbits, and Snails Board GameAsana · ONSITE INTERVIEW
- Ultimate Tic-Tac-Toe Move SimulatorHebbia · PHONE SCREEN
- Transformer Attention and SwiGLU Forward PassGoogle · ONSITE INTERVIEWGoogle interview questions
- Self-Attention and Causal Masked Self-AttentionMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions