FastPrepSelf-Attention and Causal Masked Self-Attention
Problems/Self-Attention and Causal Masked Self-Attention

Self-Attention and Causal Masked Self-Attention

Microsoft logoMicrosoft● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Microsoft Senior Applied Scientist onsite report asks the candidate to write self-attention and masked self-attention. It gives no tensor shapes, projections, scaling rule, mask format or tolerance for numbers. Here, we start with Q, K and V rows already worked out, use one head, a stable softmax and a causal mask. Our version is about 80% the same as the reported question.

Implement single-head scaled dot-product self-attention over one sequence. The rows of query, key, and value correspond to the same n sequence positions; they are already projected, so no learned weight matrices are supplied.

For query position i, score key position j by dot(query[i], key[j]) / sqrt(d), where d is the query/key width. Apply maximum-subtracted softmax to the allowed scores, then return the weighted sum of the corresponding value rows.

When causal is false, every position may attend to every key. When causal is true, query position i may attend only to positions 0..i, including itself. Return an n by v matrix. There is no batch dimension, additional attention head, bias, dropout, or backward pass. Compare each returned entry with absolute tolerance 1e-6.

Function

selfAttention(query: double[][], key: double[][], value: double[][], causal: boolean) → double[][]

Examples

Example 1

query = [[0.0],[0.0]]key = [[1.0],[2.0]]value = [[4.0],[8.0]]causal = falsereturn = [[6.0],[6.0]]

Without a mask, both zero query rows give equal weight to values 4 and 8.

Example 2

query = [[0.0],[0.0]]key = [[1.0],[2.0]]value = [[4.0],[8.0]]causal = truereturn = [[4.0],[6.0]]

The first row may attend only to position zero; the second sees both positions.

Example 3

query = [[1.0,0.0],[0.0,1.0]]key = [[1.0,0.0],[0.0,1.0]]value = [[2.0,0.0],[0.0,4.0]]causal = falsereturn = [[1.339523098653,1.320953802693],[0.660476901347,2.679046197307]]

Each query favors its matching key after scaling by the square root of two.

Constraints

  • 1 <= n <= 20, 1 <= d, v <= 8.
  • query and key are rectangular n by d matrices.
  • value is a rectangular n by v matrix.
  • All entries are finite real numbers between -20 and 20, inclusive.
  • causal is a boolean flag selecting the mask.

More Microsoft Onsite Interview questions

  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · 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
  • 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
  • Transformer Attention and SwiGLU Forward PassGoogle · ONSITE INTERVIEWGoogle 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

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public double[][] selfAttention(double[][] query, double[][] key, double[][] value, boolean causal) {
        // Write your code here.
    }
}
Checking account…