The interview problem
Explain why common events should receive shorter codes, calculate entropy for a small distribution and distinguish true entropy from cross-entropy under a mismatched model.
Original practice, not a reported company interview. All scenarios and worked numbers are illustrative. Interview date: not applicable. Sources retrieved September 30, 2026.
1. Scope and assumptions
Use base-two logarithms and a discrete distribution with positive probabilities for observed symbols. The coding interpretation assumes an appropriate lossless code; entropy is not a direct measure of semantic importance.
2. Worked approach
Entropy H(P)=−sum p log₂p is expected information under the true distribution. Cross-entropy H(P,Q)=−sum p log₂q is the expected log loss from predicted distribution Q. Their difference is KL(P||Q), when defined, and is nonnegative.
For prefix codes, assign codewords so none prefixes another. Integer-length single-symbol codes need not achieve entropy exactly; block or arithmetic coding approaches the theoretical rate under suitable conditions. The probability model and codebook must be shared or transmitted, so short datasets include overhead beyond ideal symbol costs.
3. Calculate the example
For P=(1/2,1/4,1/4), entropy is 0.5×1+0.25×2+0.25×2=1.5 bits. Codes 0,10,11 achieve that average. Using Q=(1/3,1/3,1/3) has cross-entropy log₂3≈1.585 bits, about 0.085 extra bits per symbol. Over one million independent symbols the ideal excess is about 84,963 bits, or 10,620 bytes, excluding framing and model overhead.
4. Failure cases and trade-offs
A predicted zero probability for an event with positive true probability gives infinite log loss. Confusing natural logs with base-two logs changes units. A low-entropy label distribution can still contain rare events with high business cost.
A richer probability model can reduce symbol cost but adds storage, compute and adaptation overhead. Smoothing prevents infinite loss at a possible cost on frequent symbols. Optimize a task-specific decision metric separately when log loss does not capture the product's costs.
5. Validate the result
Verify probabilities sum to one, compare a matching Q=P with a mismatched Q, and reconstruct the prefix-code average. Measure actual encoded size as well as theoretical cross-entropy on held-out data.
6. Interview follow-ups and self-review
Why can a deterministic distribution have zero entropy? What changes when symbols are dependent?
Reward units, prefix validity, correct cross-entropy arithmetic and model-overhead caveats.
Sources and related practice
Technical reference: docs.scipy.org — Official entropy/KL definitions; codebook and storage calculations are original.
Exact publication/update date not established in this review. Retrieved September 30, 2026. This reference supports the named technical concept; the proposed workflow and examples are original, not a documented vendor implementation or interview report.
Continue with Choose the direction of KL divergence; Interpret mutual information and dependence.
Continue your preparation
Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.