Compute variance of a list in Python
Company: PayPal
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of statistical measures and numeric computation, focusing on implementing variance calculations (population vs sample) and considerations for numerical stability.
Constraints
- 1 <= n <= 10^6
- Each element is an int or float.
- Use population variance (divide by n), not sample variance.
- Do not use numpy or pandas.
Examples
Input: ([2, 4, 6, 8],)
Expected Output: 5.0
Explanation: mean=5; squared deviations 9,1,1,9 sum to 20; 20/4 = 5.0.
Input: ([1, 1, 1, 1],)
Expected Output: 0.0
Explanation: All elements equal the mean, so every deviation is 0.
Input: ([5],)
Expected Output: 0.0
Explanation: Single element: mean=5, only deviation is 0, variance 0.
Input: ([-2, -4, -6, -8],)
Expected Output: 5.0
Explanation: mean=-5; deviations 3,1,1,3 squared sum to 20; 20/4 = 5.0 (negatives handled).
Input: ([1, 2, 3, 4, 5],)
Expected Output: 2.0
Explanation: mean=3; squared deviations 4,1,0,1,4 sum to 10; 10/5 = 2.0.
Input: ([10, 20],)
Expected Output: 25.0
Explanation: mean=15; deviations 5,5 squared sum to 50; 50/2 = 25.0.
Hints
- First compute the mean = sum(nums) / n.
- Then sum the squared differences (x - mean)^2 over all elements.
- Divide that sum by n (population variance). For n == 1 the result is 0.