Coding questions · PayPal
Compute Variance from a Python List
This question evaluates understanding of basic statistical concepts and numerical computation—specifically population versus sample variance—and tests algorithmic implementation skills, edge-case handling, and complexity reasoning within the Coding & Algorithms domain.
Given a Python list of numeric values, write a function to compute the variance without using external libraries such as NumPy or pandas.
Clarify any assumptions you make, such as:
- whether you are computing population variance or sample variance,
- how to handle an empty list or a list with one element,
- and the expected time and space complexity.
Compute Population Variance from a Python List
Return population variance rounded to 6 decimals; return None for an empty list.
Examples
Example 1
Input: ([1,2,3],)
Output: 0.666667
Explanation: Population variance.
Example 2
Input: ([5,5,5],)
Output: 0.0
Explanation: Zero variance.
Hints
- Model object-style prompts as arrays or operation streams when needed.
- Handle empty and boundary cases before the main logic.