Streaming Entropy, Part 2: Numerically Stable Entropy
Learn this problemProblem statement
Streaming Entropy Interview Sequence
This is Part 2 of one four-part ML coding exercise.
- Streaming Entropy, Part 1: Batch Entropy
- Streaming Entropy, Part 2: Numerically Stable Entropy
- Streaming Entropy, Part 3: Block-wise Entropy
- Streaming Entropy, Part 4: Stable Streaming Entropy
As in Part 1, logits define a softmax distribution:
p[i] = exp(x[i]) / sum(exp(x[j]))
and its entropy is
H = -sum(p[i] * ln(p[i]))
This time the logits may be extremely large or small. Directly computing exp(x[i]) can overflow, and computing log(exp(x[i]) / sumExp) can take the logarithm of a probability that rounded to zero.
Numerical Stability Requirements
Return the non-negative entropy without overflow, underflow-driven NaN, or loss of the log-softmax term.
Use a maximum shift for the exponentials and derive log-softmax algebraically from the shifted logits and their normalization sum. Your implementation should run in O(n) time.
An answer is accepted when its absolute or relative error is at most 1e-6.
Continue to Part 3: Block-wise Entropy.
Function
stableEntropy(logits: double[]) → doubleExamples
Example 1
logits = [1000.0, 1000.0, -1000.0]return = 0.6931471805599453Directly evaluating exp(1000) overflows. After shifting by the maximum, the third probability is negligible and the first two are each effectively 0.5.
Example 2
logits = [999999998.0, 999999999.0, 1000000000.0]return = 0.8323955818399389Subtracting the maximum turns the logits into [-2, -1, 0] without changing the softmax distribution.
Example 3
logits = [42.0]return = 0.0A one-element softmax distribution is [1], whose entropy is zero.
Constraints
1 <= logits.length <= 2 * 10^5- Every logit is finite and lies in
[-10^9, 10^9]. - Use the natural logarithm.
- Use
O(n)time. - The result must remain finite for every valid input.
More OpenAI problems
- Memory AllocatorPHONE SCREEN · Seen Jul 2026
- Message Event AggregationONSITE INTERVIEW · Seen Jul 2026
- Plant Infection Simulation, Part 4: Death CountdownPHONE SCREEN · Seen Jun 2026
- Streaming Entropy, Part 1: Batch EntropyONSITE INTERVIEW · Seen Jun 2026
- Streaming Entropy, Part 3: Block-wise EntropyONSITE INTERVIEW · Seen Jun 2026
- Streaming Entropy, Part 4: Stable Streaming EntropyONSITE INTERVIEW · Seen Jun 2026
- Resumable List IteratorPHONE SCREEN · Seen Jun 2026
- Versioned Social NetworkONSITE INTERVIEW · Seen May 2026