Ice Cream Forecast Check
A beach kiosk uses a small model to predict each day's ice-cream sales. At the end of the week the owner has two lists, predicted and actual, and wants one number for how far off the model was. A miss of 40 cones means melted stock or angry customers, so big misses should count much more than small ones. Training a model means making a number like this smaller, step by step.
Your task
Write forecastError(predicted, actual). For each day, the miss is predicted[i] - actual[i]. Square every miss, then return the average of those squares.
The two lists always have the same length. If both are empty, return 0.
forecastError(predicted: float[], actual: float[]) → float Numbers are accepted within 1e-6.
Examples
predicted = [120, 80, 150] actual = [100, 90, 150]
166.66666666666666
Misses 20, -10 and 0 square to 400, 100 and 0. The average is 500 / 3 = 166.666…
predicted = [50, 70] actual = [60, 60]
100
The misses are -10 and +10. Averaging them first gives 0, as if the model were perfect. Squaring first gives 100 and 100, so the answer is 100.
Constraints
- 0 ≤ predicted.length = actual.length ≤ 10,000
- -1,000,000 ≤ predicted[i], actual[i] ≤ 1,000,000
- Answers within 0.000001 of the expected value (or relatively that close) are accepted.
Start with brute force
There is no slow way: you must look at every day once, and one loop does that. The traps are wrong formulas. Averaging the misses and then squaring lets them cancel. Absolute values give a different measure (mean absolute error) that punishes big misses less. Forgetting to divide gives a total that grows with the number of days.
Hints
0 of 4 shown- Hint 1
In the second example the model was 10 too low one day and 10 too high the next. What happens if you add the misses up first?
- Hint 2
Positive and negative misses cancel when added. Squaring each miss on its own makes every miss a cost, whatever its sign.
- Hint 3
Squaring also makes a miss of 20 cost 400 while a miss of 10 costs only 100, so big misses are punished more.
- Hint 4
Loop once, keeping a running total of
(predicted[i] - actual[i])², then divide by the number of days. Handle the empty case before dividing.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Mean squared error is three steps: find each miss, square it, average the squares. Square before you add, or opposite misses cancel.
Trace predicted = [120, 80, 150], actual = [100, 90, 150]:
- On day 0 the miss is 20, its square is 400, and the running total is 400.
- On day 1 the miss is -10, its square is 100, and the running total is 500.
- On day 2 the miss is 0, its square is 0, and the total stays 500.
- Dividing 500 by the 3 days gives 166.666…
The 20-cone miss produced 400 of the 500. Squaring makes the model care most about its worst days, so training on this loss pushes hardest to fix them.
function forecastError(predicted, actual) {
const n = predicted.length;
if (n === 0) return 0;
let total = 0;
for (let i = 0; i < n; i++) {
const miss = predicted[i] - actual[i];
total += miss * miss; // square each miss on its own
}
return total / n;
}def forecast_error(predicted, actual):
n = len(predicted)
if n == 0:
return 0.0
total = 0.0
for p, a in zip(predicted, actual):
miss = p - a
total += miss * miss # square each miss on its own
return total / nRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.