Problem: Moving Average from Data Stream
Explore how to implement a MovingAverage class in C# that calculates the moving average of the latest values from a data stream using a queue. Understand how to efficiently maintain a sliding window with constant time complexity for updates, handling variable window sizes and stream input.
We'll cover the following...
Statement
Given a stream of integers and a window size, calculate the moving average of all integers within the sliding window.
Implement the MovingAverage class:
MovingAverage(int size): Initializes the object with the window sizesize.double Next(int val): Accepts a new integervalfrom the stream and returns the moving average of the most recentsizevalues seen so far.
Note: If fewer than
sizevalues have been received so far, the moving average is computed over all the values received up to that point.
Constraints:
size...