dsa · easy

Moving Average from Data Stream

GreyOrangeQueueDesignFoundation

MovingAverage(size). Each next(val) returns the average of the last size values (or all values so far if fewer than size). Return a float.

Fill in the MovingAverage class. The starter already walks ops / args and calls your methods — leave the driver at the bottom as-is. Constructors contribute null; booleans print as true / false.

**Example**

`` MovingAverage(3) → null next(1) → 1.0 next(10) → 5.5 next(3) → 4.666666666666667 next(5) → 6.0 ``

Constraints

1 <= size <= 1000, calls <= 1000

Examples

Example 1

Input:
["MovingAverage","next","next","next","next"]
[[3],[1],[10],[3],[5]]

Expected:
[null,1.0,5.5,4.666666666666667,6.0]

Open in the Dojo editor