Search⌘ K
AI Features

Solution: Moving Average from Data Stream

Explore how to compute the moving average of integers from a continuous data stream using a fixed-size sliding window. This lesson guides you through implementing a MovingAverage class in Go that uses a queue and running sum to efficiently update averages in constant time as new data arrives. Understand how to manage window size constraints, perform enqueue and dequeue operations, and maintain time and space complexity for optimal performance.

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 ...