Streaming Entropy, Part 2: Numerically Stable Entropy
Reported by candidates from OpenAI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
OpenAI's July 2026 OA reports include a four-part ML coding exercise, and Part 2 is where the logits show up at 1000 and 999999999. Naive softmax dies right there. This is the numerically stable entropy step: shift by the max, compute log-softmax algebraically, and sum in O(n). If you're taking this OA in the next few days, the math is short but the traps are real. StealthCoder is the safety net if you blank on the algebra during the live assessment, but you should be able to hold this one in your head after reading below.
The problem
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[]) → double Examples Example 1 logits = [1000.0, 1000.0, -1000.0] return = 0.6931471805599453 Directly 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.8323955818399389 Subtracting the maximum turns the logits into [-2, -1, 0] without changing the softmax distribution. Example 3 logits = [42.0] return = 0.0 A 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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is log-sum-exp. Let m = max(x), s = sum(exp(x[i] - m)), and L = ln(s). Then log p[i] = (x[i] - m) - L and p[i] = exp(x[i] - m) / s. Entropy is H = -sum(p[i] * logp[i]). Every exponent is at most 0, so nothing overflows, and the max element contributes exp(0) = 1, so s is at least 1 and L is never undefined. The common pitfall is computing log(p[i]) after p[i] has underflowed to zero, which gives -inf times 0 and a NaN. Don't take the log of p. Use the algebraic form instead. Example 1 shows it: the -1000 logit gives p near 0, but its log-softmax term is finite, so the product vanishes cleanly. Single-element input returns 0. Watch for -0.0 and clamp tiny negatives to 0. StealthCoder is your hedge if the live OA rattles you, but this is a three-line formula once you see it.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Streaming Entropy, Part 2: Numerically Stable Entropy cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass OpenAI's OA.
OpenAI reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Streaming Entropy, Part 2: Numerically Stable Entropy FAQ
What's the trick in Streaming Entropy Part 2?+
Subtract the max logit before exponentiating, then compute log-softmax as (x[i] - max) - ln(sumExp). Never take the log of a probability you've already computed, because it may have underflowed to zero. Entropy is then -sum(p[i] * logSoftmax[i]), all in one O(n) pass after the shift.
Why does the max shift not change the answer?+
Softmax is invariant to adding a constant to every logit, since the factor exp(-m) cancels between numerator and denominator. Example 2 shows it: [999999998, 999999999, 1000000000] becomes [-2, -1, 0] with the same distribution and the same entropy of about 0.8324.
What edge cases should I test before submitting?+
Test a single element, which must return 0.0. Test all-equal logits, which should give ln(n). Test a huge spread like [1000, 1000, -1000], where one term underflows. Also check for tiny negative results from floating point, and clamp them to 0 since entropy is non-negative.
How hard is this really?+
Easy if you know log-sum-exp, annoying if you don't. There's no data structure or clever algorithm, just numerical care. The input can reach 2 * 10^5 elements, so a single linear scan for the max plus one pass for the sum and entropy is plenty.
How do I prep for this in 48 hours?+
Write stable softmax and log-softmax from memory once. Then derive entropy from them and run the three examples by hand. Since this is Part 2 of four, also skim how Part 3 (block-wise) and Part 4 (stable streaming) would reuse the same max-and-sum idea, so you're not surprised by the follow-ups.