FastPrepSoftmax Cross-Entropy Loss
Problems/Softmax Cross-Entropy Loss

Softmax Cross-Entropy Loss

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Microsoft Research + AI Research internship report asks candidates to implement softmax cross-entropy from logits to loss. Integer class labels, mean batch reduction, stable log-sum-exp, bounds, and tolerance are conventional enrichment selected from the source's abbreviated contract.

You are given a batch of logits for a classification task. Each row holds one score per class. labels gives the index of the correct class for each row. Return the mean softmax cross-entropy loss over all rows.

For row i, the loss is log(sum(exp(logits[i][j]))) - logits[i][labels[i]], where the sum runs over every j in that row. Before taking the exponentials, subtract the largest logit in the row from every value in that row. This keeps the exponentials from growing too large, so the result stays numerically stable.

Function

softmaxCrossEntropy(logits: double[][], labels: int[]) → double

Examples

Example 1

logits = [[1.0,2.0,3.0],[1.0,1.0,1.0]]labels = [2,0]return = 0.7531091265562451

In the first row, the correct class has the largest logit, and its loss is about 0.407606. The second row has equal logits, so its loss is log(3). The mean of the two row losses is about 0.753109.

Example 2

logits = [[1000.0,999.0]]labels = [0]return = 0.31326168751822286

Subtract 1000, the largest logit, from both values. The exponentials become 1 and e^-1, so nothing overflows. The correct class has the largest logit, so after the shift its value is zero. The loss is the log of the sum of the two exponentials, which is the output shown.

Constraints

  • 1 <= logits.length = labels.length <= 200
  • 2 <= logits[i].length <= 100, and every row has the same length
  • -1000 <= logits[i][j] <= 1000, and every logit is finite
  • 0 <= labels[i] < logits[i].length
  • Your answer is accepted if it is within an absolute difference of 1e-9 from the expected value

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

  • Random Bingo Card GeneratorUber · ONSITE INTERVIEWUber interview questions
  • Streaming Entropy, Part 2: Numerically Stable EntropyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
  • Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Maximize the Even-Odd Index Product GapInfosys · ONSITE INTERVIEWInfosys interview questions
  • Squirrel Nut Collection Shortest PathAmazon · ONSITE INTERVIEW
  • Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions
  • Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions
  • Product Total With the Cheapest PromotionInstacart · ONSITE INTERVIEWInstacart interview questions
  • Reservoir Sampling with Recorded DrawsStackAdapt · ONSITE INTERVIEWStackAdapt interview questions

Interview reports that mention this question

  • Microsoft AI/ML Intern Interviews: 114 Experiences (Part 1 of 2)

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)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public double softmaxCrossEntropy(double[][] logits, int[] labels) {
      // Write your code here.
    }

}
Checking account…