Back to problems

Moving Average over a Sliding Window

Algorithm · Coinbase · Easy

Design a class MovingAverage that processes a stream of integers and computes the average over a sliding window. The constructor accepts an integer window size k. Each call to next(val) appends val to the stream and returns the arithmetic mean of the most recent values, up to k values. If fewer than k values have been added so far, return the average of all available values. If the constructor receives k <= 0, it must raise an exception. Input The first line contains two…

Checking your access…