Tap here to switch tabs
Problem
Submissions

Problem: Moving Average from Data Stream

easy
15 min
Explore how to implement a MovingAverage class that efficiently computes the average of the last 'size' values from a stream of integers. Understand sliding window techniques and work through coding exercises to solidify your grasp of custom data structures designed for streaming data.

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 100\leq 100

  • 103-10^3 \leq val 103\leq 10^3

  • At most 10210^2 calls can be made to next.

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
Tap here to switch tabs
Problem
Submissions

Problem: Moving Average from Data Stream

easy
15 min
Explore how to implement a MovingAverage class that efficiently computes the average of the last 'size' values from a stream of integers. Understand sliding window techniques and work through coding exercises to solidify your grasp of custom data structures designed for streaming data.

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 100\leq 100

  • 103-10^3 \leq val 103\leq 10^3

  • At most 10210^2 calls can be made to next.

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths