346. Moving Average from Data Stream - Detailed Explanation

Problem Statement

Implement a class MovingAverage that calculates the moving average of the last size elements from a stream of integers. The class should support two operations:

  • MovingAverage(int size): Constructor to initialize the object with the window size.
  • double next(int val): Returns the moving average of the last size values of the data stream (including the new value val).

Example Test Cases

Example 1:

MovingAverage obj = new MovingAverage(3); obj.next(1); // returns 1.0 obj.next(10); // returns (1 + 10) / 2 = 5.5 obj.next(3); // returns (1 + 10 + 3) / 3 = 4.67 obj.next(5); // returns (10 + 3 + 5) / 3 = 6.0

Example 2:

MovingAverage obj = new MovingAverage(2); obj.next(5); // returns 5.0 obj.next(15); // returns (5 + 15) / 2 = 10.0 obj.next(10); // returns (15 + 10) / 2 = 12.5

Hints to Solve

  1. Use a data structure to store the last size values and allow easy addition/removal of elements.
  2. Maintain a running sum so you can compute the new average quickly without summing all elements from scratch each time.

Approaches

Brute Force Approach (Using an Array/List)

  • Keep a list of all values seen so far.
  • On each call to next(val), add the new value to the list and then compute the sum of the last size elements in the list.
  • Return the average as this sum divided by size (or by the number of elements if fewer than size have been seen so far).
  • Time Complexity: O(size) per query (each next call requires iterating over up to size elements for the sum).

Optimized Approach (Using a Queue – Sliding Window)

  • Use a queue (or deque) to store the most recent size values. This naturally discards the oldest value when the size is exceeded.
  • Maintain a running sum of the values in the queue (the current window sum).
  • On each next(val) call:
    • If the queue is already at the maximum size, remove (popleft()/poll()) the oldest value and subtract it from the running sum.
    • Add the new value to the queue and add it to the running sum.
    • Compute the average as running_sum / current_window_length.
  • Time Complexity: O(1) per query (each next operation does a constant amount of work, regardless of the window size).

Code Implementations

Python Solution (Queue Approach)

Python3
Python3

. . . .

Java Solution (Queue Approach)

Java
Java

. . . .

Complexity Analysis

  • Brute Force Approach: Each call to next is O(n) where n = size of the window. This can be inefficient when size is large, because computing the sum of the window on every call takes linear time.

  • Optimized Queue Approach: Each call to next is O(1). We only do a constant amount of work (one addition and possibly one removal), regardless of the window size. This is optimal for this problem.

Common Mistakes

  • Not removing the oldest element: Forgetting to drop the oldest value when the window exceeds the specified size will lead to incorrect averages (and an ever-growing window).

  • Handling the first few elements: Not considering the case when fewer than size elements have been seen. In those cases, the average should be taken over all elements that are present so far (the window hasn't fully filled yet).

Edge Cases

  • Window Size 1: Every new value should simply be returned as the average (since the window contains only that element).
  • Extreme Values: The values can be as low as -10^4 or as high as 10^4. Ensure that the sum (which could be as large as size * 10^4) fits in the chosen data type without overflow (in Python it's fine; in Java, an int might overflow if size is large, so using a double or long for the sum is safer).

Alternative Variations

  • Weighted Moving Average: Instead of an unweighted average, each element could have a weight (for example, more recent elements weighted more heavily than older ones).

  • Missing Values: How to handle cases where the data stream might have missing values or nulls—perhaps skipping them or treating them as zeros, depending on the specification.

  • Sliding Window Maximum – Find the maximum in each sliding window of size k in an array.
  • Design Hit Counter – Design a system that counts hits received in the past 5 minutes using a sliding window approach.
  • Running Sum of 1d Array – Although not a sliding window, it involves maintaining a running sum, which is a similar concept.
TAGS
leetcode
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
176. Second Highest Salary - Detailed Explanation
Learn to solve Leetcode 176. Second Highest Salary with multiple approaches.
1086. High Five - Detailed Explanation
Learn to solve Leetcode 1086. High Five with multiple approaches.
55. Jump Game - Detailed Explanation
Learn to solve Leetcode 55. Jump Game with multiple approaches.
2017. Grid Game - Detailed Explanation
Learn to solve Leetcode 2017. Grid Game with multiple approaches.
322. Coin Change - Detailed Explanation
Learn to solve Leetcode 322. Coin Change with multiple approaches.
286. Walls and Gates - Detailed Explanation
Learn to solve Leetcode 286. Walls and Gates with multiple approaches.
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.