Easy · Stacks & Queues

Find moving average in sliding window

Given a window size k ≥ 1 and a stream of integers passed one at a time to next(val), return the result of every call: the average of the last k values seen, or of all values seen while fewer than k have arrived.

Examples

Example 1

k=3 · [1,10,3,5]

Output: [1, 5.5, 4.667, 6]

Example 2

k=2 · [4,4,8,2,6]

Output: [4, 4, 6, 5, 4]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Stacks & Queues problems