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
| Method | Behaviour |
|---|---|
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(), andpercentile(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.