Search⌘ K
AI Features

Solution: Moving Average from Data Stream

Understand how to build a MovingAverage class that calculates the moving average of integer streams using a sliding window approach. Explore queue-based data structures and efficient algorithms to maintain a running sum and update averages in constant time, enhancing your skill in custom JavaScript data structures.

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 size values from the stream.

Constraints:

  • 11 \leq size ...