Numerically Stable Softmax
Updated
Problem statement
Write softmax by hand, in a way that stays numerically stable. You are given a matrix logits. Each row is its own list of scores. Return a matrix of the same shape in which every row is turned into probabilities: the values of a row are between 0 and 1 and add up to 1.
For one row x with c values, the probability at position j is exp(x[j]) / (exp(x[0]) + exp(x[1]) + ... + exp(x[c - 1])).
Logits here go up to 104 in size. Computed as written, exp overflows for large logits and rounds to 0 for very negative ones, so your code must give the correct probabilities without ever overflowing or dividing 0 by 0.
Write the arithmetic yourself; do not call a library softmax function.
Function
softmax(logits: double[][]) → double[][]Examples
Example 1
logits = [[1.0,2.0,3.0],[0.0,0.0,0.0]]return = [[0.09003057317038046,0.24472847105479764,0.6652409557748218],[0.3333333333333333,0.3333333333333333,0.3333333333333333]]Row 1: the exps are [2.7183, 7.3891, 20.0855], which add up to 30.1929. Dividing gives [0.0900, 0.2447, 0.6652]. Row 2: all logits are equal, so each gets 1/3.
Example 2
logits = [[1000.0,999.0],[-1000.0,-1002.0]]return = [[0.7310585786300049,0.2689414213699951],[0.8807970779778823,0.11920292202211755]]Computed as written, exp(1000) overflows and exp(-1000) becomes 0, so both rows would come out as invalid values or raise an error. A stable version still returns the true probabilities: row 1 is [0.7311, 0.2689] and row 2 is [0.8808, 0.1192].
Example 3
logits = [[-7.25],[10000.0]]return = [[1.0],[1.0]]A row with one logit always gets probability 1, whatever the value.
Constraints
1 <= logits.length <= 201 <= logits[i].length <= 50, and every row has the same length.-104 <= logits[i][j] <= 104, with at most two digits after the decimal point.- Each returned value is accepted if it is within
10-12of the expected value.
More Waymo Phone Screen questions
- 2D Convolution Layer Forward PassPHONE SCREEN · Seen Dec 2026
- Multi-Head Attention Forward PassPHONE SCREEN · Seen Dec 2026
- Shortest Travel Time in a Grid with Traffic LightsPHONE SCREEN · Seen Nov 2026
- Fixed-Order Task Run Time With CooldownPHONE SCREEN · Seen Oct 2026
- Reorderable Task Scheduler With CooldownPHONE SCREEN · Seen Oct 2026
- Island Area and Water BoundaryPHONE SCREEN · Seen Sep 2026
- Vehicle Maintenance Event ReadinessPHONE SCREEN · Seen Sep 2026
- Obstacle-Aware Return to OriginPHONE SCREEN · Seen Sep 2026
- Shortest Paths to Multiple TargetsPHONE SCREEN · Seen Sep 2026
- Deterministic Depth-First Graph TraversalPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Streaming Entropy, Part 1: Batch EntropyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Merchant Payout Reserve CalculatorStripe · PHONE SCREENStripe interview questions
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Length of a Document Reached Through a Random TokenMercor · PHONE SCREENMercor interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Exponential Backoff DelaysWonderful · PHONE SCREEN
- Recover Document Hierarchy from IndentationReducto · PHONE SCREEN
- Average Grades per StudentHartford Financial Services · PHONE SCREENHartford Financial Services interview questions
- Through-Origin Linear RegressionMeta · PHONE SCREENMeta interview questions