FastPrepCompute Population Variance

Compute Population Variance

Microsoft logoMicrosoft● MediumNEW GRADFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a nonempty array of real-valued observations values, return their population variance.

Population variance is the average squared distance from the population mean. Compute the result in one pass over the input.

Function

populationVariance(values: double[]) → double

Examples

Example 1

values = [1.0,2.0,3.0,4.0]return = 1.25

The mean is 2.5. The squared deviations sum to 5, and 5 / 4 = 1.25.

Example 2

values = [2.0,2.0,2.0]return = 0.0

Every observation equals the mean, so every squared deviation is zero.

Constraints

  • 1 <= values.length <= 100000
  • -10^6 <= values[i] <= 10^6
  • The answer is accepted within 10^-9 absolute error.

More Microsoft problems

See Microsoft hiring insights
public double populationVariance(double[] values) {
  // Write your code here.
}
values[1.0,2.0,3.0,4.0]
expected1.25
Checking account…