Streaming Entropy, Part 1: Batch 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 report is Part 1 of a four-part ML coding sequence, and it looks gentle. Compute the entropy of a softmax over up to 200,000 logits. It's a math and array problem, not a clever algorithm. The catch is the O(n) requirement and the fact that this is just the warm-up for stability and streaming variants. If you blank on the formula under pressure, StealthCoder runs invisibly during the live assessment and gives you a working solution. But this one is short enough to hold in your head.
The problem
Streaming Entropy interview sequence This is Part 1 of one four-part ML coding exercise. Part 1: Batch Entropy Part 2: Numerically Stable Entropy Part 3: Block-wise Entropy Part 4: Stable Streaming Entropy A model produces a one-dimensional array of logits x[0], x[1],..., x[n - 1]. The logits define a probability distribution through softmax: p[i] = exp(x[i]) / sum(exp(x[j])) The entropy of that distribution is: H = -sum(p[i] * ln(p[i])) Here, ln is the natural logarithm. Given all logits at once, return the non-negative entropy of their softmax distribution. An answer is accepted when its absolute or relative error is at most 1e-6. This opening part uses moderate logits, so numerical overflow and underflow are not the focus yet. Your implementation should run in O(n) time. After solving this part, continue to Part 2: Numerically Stable Entropy. Function entropy(logits: double[]) → double Examples Example 1 logits = [0.0, 0.0] return = 0.6931471805599453 The two logits are equal, so softmax produces [0.5, 0.5]. The entropy is -2 * 0.5 * ln(0.5) = ln(2). Example 2 logits = [0.0, 1.0986122886681098] return = 0.5623351446188083 The second logit is ln(3), so the probabilities are [0.25, 0.75]. Example 3 logits = [2.0, 1.0, 0.0] return = 0.8323955818399389 Softmax is unchanged if the same constant is added to every logit. This distribution is equivalent to the logits [0, -1, -2]. Constraints 1 <= logits.length <= 2 * 10^5 Every logit is finite and lies in [-20, 20]. Use the natural logarithm. Use O(n) time.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is algebra. Entropy is H = -sum(p * ln p), and ln p[i] = x[i] - ln(Z), where Z is the sum of exp(x[j]). So H = ln(Z) - sum(p[i] * x[i]). That's two passes: one to compute Z, one to compute the weighted sum of logits. No need to call ln on every probability, which also dodges ln(0) worries. The pitfall is a nested loop that recomputes the softmax sum per element, which is O(n^2) and dies at 2 * 10^5. Logits sit in [-20, 20], so plain exp is safe here. Still, subtract the max anyway, because Part 2 is about exactly that and it costs you nothing. Return a double and trust the 1e-6 tolerance. If you freeze mid-OA, StealthCoder is the hedge that keeps you moving.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Streaming Entropy, Part 1: Batch 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass OpenAI's OA.
OpenAI reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Streaming Entropy, Part 1: Batch Entropy FAQ
How hard is the OpenAI Batch Entropy question really?+
Easy on algorithm, easy to fumble on details. It's a single O(n) pass with some math. The risk is writing an O(n^2) softmax or mishandling the log. If you know the entropy formula, you can finish in a few minutes.
What's the trick to keep it O(n)?+
Compute the normalizer Z once, then get probabilities by dividing. Even better, use H = ln(Z) - sum(p[i] * x[i]). That's two linear passes and no per-element recomputation of the denominator.
Do I need the max-subtraction stability trick here?+
Not strictly. Logits are bounded in [-20, 20], so exp won't overflow. But subtracting the max is free, gives the same answer, and it's exactly what Part 2 asks for. Do it now and you've done half of the next part.
What precision do I need to match?+
Absolute or relative error of 1e-6. Use double everywhere and natural log. Check your output against the examples: two zeros should give 0.6931471805599453, and [2, 1, 0] should give about 0.83239558.
How do I prepare for this in 48 hours?+
Write softmax and entropy from scratch once, then rewrite it with the max-subtraction. Then think about how you'd stream it, since the later parts build on this. Test edge cases: length 1 gives entropy 0, and equal logits give ln(n).