Problem: Moving Average from Data Stream
Explore how to implement a MovingAverage class in JavaScript that calculates the sliding window average for incoming integer streams. Learn to use a queue data structure with a running sum for efficient real-time computation, optimizing to O(1) time complexity for each new value. This lesson helps you apply queues to maintain fixed-size windows and master an important data stream problem.
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...