Skip to main content
πŸ’Ή Finance & Math Tools

What you're building

A StatsEngine class that accepts numbers one at a time and always has the current mean, median, and population standard deviation available β€” without re-scanning the whole dataset.

const stats = new StatsEngine();
stats.push(10);
stats.push(20);
stats.push(30);
stats.mean();   // 20
stats.median(); // 20
stats.stdev();  // 8.165... (population std dev)

Requirements

MethodBehaviour
push(value)Add a number to the dataset
mean()Return the arithmetic mean; null if empty
median()Return the median; null if empty
stdev()Return the population standard deviation; 0 if fewer than 2 values
count()Return how many values have been pushed

Population std dev formula:

Οƒ = sqrt( Ξ£(xα΅’ βˆ’ mean)Β² / N )

For median: sort the current dataset, take the middle value for odd N, or the average of the two middle values for even N.

Return all floating-point results as-is (no forced rounding).

Stretch goals

  • Implement Welford's online algorithm for numerically stable variance (no array needed for mean/stdev)
  • Add min(), max(), and percentile(p) methods

What this proves

Real-time analytics, statistical formulas in code, and clean class design β€” appearing in data engineering, quant finance, and ML monitoring interviews.

⚑ 60 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
mean of [10, 20, 30] is 20
median of even-length dataset
stdev of [2, 4, 4, 4, 5, 5, 7, 9] is 2
mean and median are null when empty

Run your code to check these.