Streaming Entropy, Part 3: Block-wise Entropy
Learn this problemProblem statement
Streaming Entropy Interview Sequence
This is Part 3 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
The logits of one softmax distribution now arrive in consecutive, non-empty blocks. Concatenating the blocks in order would produce the complete logits array.
For the full stream,
p[i] = exp(x[i]) / sum(exp(x[j]))
H = -sum(p[i] * ln(p[i]))
Streaming Requirements
Process the blocks in order and return the entropy over every logit in the stream.
Do not concatenate the blocks, retain the full logits stream, or materialize the probability vector. Keep only constant-size aggregate state. This part uses moderate logits so that block-wise processing, rather than numerical stability, is the main challenge.
Your implementation must run in O(n) time and use O(1) auxiliary space, where n is the total number of logits.
An answer is accepted when its absolute or relative error is at most 1e-6.
Continue to Part 4: Stable Streaming Entropy.
Function
blockwiseEntropy(blocks: double[][]) → doubleExamples
Example 1
blocks = [[0.0], [0.0]]return = 0.6931471805599453The blocks form the logits [0, 0], which produce a uniform two-outcome distribution.
Example 2
blocks = [[0.0], [1.0986122886681098]]return = 0.5623351446188083The blocks form logits whose softmax probabilities are [0.25, 0.75].
Example 3
blocks = [[1.0, -1.0, 0.0], [2.0], [-2.0, 0.5]]return = 1.2511246070440085This stream uses uneven blocks and includes positive, negative, and fractional logits.
Constraints
1 <= blocks.length <= 10^41 <= blocks[i].length- The total number of logits across all blocks is at most
2 * 10^5. - Every logit is finite and lies in
[-20, 20]. - Use the natural logarithm.
- Use
O(n)time andO(1)auxiliary space.
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 2: Numerically Stable 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