Solution: Moving Average from Data Stream
Understand how to create a MovingAverage class that calculates the moving average of integers from a data stream using a fixed-size sliding window. Learn to efficiently manage data with a queue and maintain a running sum for quick computation. This lesson helps you implement and analyze the algorithm with optimal time and space complexity.
We'll cover the following...
Statement
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. Implement a class called MovingAverage that has the following methods:
Constructor (int size): This constructor initializes the object with the specified window size.
double next (int val): This method takes an integer value as input and returns the moving average of the last
sizevalues from the stream.
Constraints:
size...