Compute and Explain Mean and Variance
Company: Amazon
Role: Machine Learning Engineer
Category: Statistics & Math
Difficulty: medium
Interview Round: Onsite
# Compute and Explain Mean and Variance
The preserved report describes a short numeric-array exercise that computes mean and variance, followed by a deeper discussion. It does not state whether variance is population or sample variance, how empty or non-finite inputs behave, or how floating-point results are compared. Explain the questions you would settle before implementation. Then, as an explicit practice assumption, analyze a nonempty finite array using population variance.
### Constraints & Assumptions
- For the worked analysis only, let `n >= 1`, use population variance, and assume finite real inputs.
- The practice convention divides squared deviations by `n`; a sample-variance contract would instead use `n - 1` where defined.
- No bitwise cross-language equality is assumed for floating-point results.
- Empty inputs, non-finite values, numeric precision, and answer tolerance must be agreed before coding.
### Clarifying Questions to Ask
- Does variance mean population variance or unbiased sample variance?
- What should happen for an empty array or a one-element sample?
- Are inputs integers or floating-point values, and what magnitude is expected?
- Must results match within a tolerance, follow one mandated operation order, or be returned exactly as rational values?
```hint Fix the statistical and numeric contracts separately
The denominator defines the estimator; the operation order and comparison policy define how a floating-point implementation can be tested.
```
### What a Strong Answer Covers
- Correct formulas for the agreed mean and variance convention
- A one-pass or two-pass algorithm with its numerical trade-offs
- Edge cases such as singleton, empty, large-offset, and non-finite data
- Why valid floating-point algorithms can differ in their last bits across languages
- Time and auxiliary-space complexity
### Follow-up Questions
1. When is Welford's online update preferable to a direct one-pass formula?
2. How would you make outputs exactly comparable if every input were an integer?
3. Why can `E[x^2] - E[x]^2` lose substantial precision?
Overview: Review mean and variance formulas, estimator conventions, stable linear-time algorithms, edge cases, and cross-language floating-point comparison limits.