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
Core interview problems are free with a full runner and lessons; Pro opens the complete library.