Implement Stable Softmax
Company: XPeng
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of numerical stability, floating-point arithmetic, normalization, and probability distributions when computing softmax outputs from real-valued inputs.
Constraints
- 0 <= n <= 100000
- -1000000000 <= x[i] <= 1000000000
- Each x[i] is a finite real number
Examples
Input: ([1.0, 2.0, 3.0],)
Expected Output: [0.09003057317038046, 0.24472847105479764, 0.6652409557748218]
Explanation: This is the standard softmax of [1, 2, 3]. The largest value gets the highest probability.
Input: ([],)
Expected Output: []
Explanation: An empty input should return an empty list.
Input: ([1000.0, 1001.0, 1002.0],)
Expected Output: [0.09003057317038046, 0.24472847105479764, 0.6652409557748218]
Explanation: A stable implementation subtracts the maximum value first, so this produces the same distribution as [1, 2, 3] without overflow.
Input: ([2.0, 2.0, 2.0],)
Expected Output: [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]
Explanation: All inputs are equal, so each position gets equal probability.
Input: ([0.0],)
Expected Output: [1.0]
Explanation: With only one element, its probability must be 1.0.
Hints
- Softmax does not change if you subtract the same constant from every element.
- Try subtracting the maximum value in the list before applying exp.