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

Implement Multi-Head Attention

ByteDance logoByteDance● HardNEW GRADFULLTIMEINTERNPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report names a timed implementation of multi-head attention. It does not say what the tensor shapes, projection weights, masking rules or number precision are, or what the output looks like. We use query, key and value matrices without projections, stable scaled dot-product attention, and output with a fixed number of decimals. That is the smallest forward pass with one answer. Our version is about 58% the same as the reported question.

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:

  1. Compute its dot product with every key slice.
  2. Divide the scores by sqrt(d / heads).
  3. Apply a numerically stable softmax across the keys.
  4. 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, and heads divides d.
  • 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

Interview reports that mention this question

  • ByteDance Trust & Safety Research Scientist Intern: 1 Experience

ByteDance interview experiences

  • ByteDance TikTok USDS SWE Intern Interview: 1 Experience
  • ByteDance MLE New Grad Interviews: 1 Experience
  • ByteDance Security SRE Intern Phone Screens: 1 Experience
  • ByteDance USDS New Grad SWE Phone Screen: 1 Experience
  • ByteDance New Grad Data Engineer Video Interviews: 1 Experience
All ByteDance interview questionsSee ByteDance hiring insights
Saved
class Solution {
    public String[] multiHeadAttention(int[][] query, int[][] key, int[][] value, int heads) {
        // Write your code here.
    }
}
Checking account…