> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Moving Average from Data Stream

> Tested Python solution for LeetCode 346 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 346, [Easy](/catalog/easy). Topics: [Design](/catalog/topics/design), [Queue](/catalog/topics/queue), [Array](/catalog/topics/array), [Data Stream](/catalog/topics/data-stream). [View on LeetCode](https://leetcode.com/problems/moving-average-from-data-stream/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 346   # by problem number
lcpy gen -s moving_average_from_data_stream   # by problem name
```

## Problem

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.

Implement the `MovingAverage` class:

* `MovingAverage(int size)` Initializes the object with the size of the window `size`.
* `double next(int val)` Returns the moving average of the last `size` values of the stream.

### Examples

```
Input
["MovingAverage", "next", "next", "next", "next"]
[[3], [1], [10], [3], [5]]
Output
[null, 1.0, 5.5, 4.66667, 6.0]

Explanation
MovingAverage movingAverage = new MovingAverage(3);
movingAverage.next(1); // return 1.0 = 1 / 1
movingAverage.next(10); // return 5.5 = (1 + 10) / 2
movingAverage.next(3); // return 4.66667 = (1 + 10 + 3) / 3
movingAverage.next(5); // return 6.0 = (10 + 3 + 5) / 3
```

### Constraints

* `1 <= size <= 1000`
* `-10^5 <= val <= 10^5`
* At most `10^4` calls will be made to `next`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/moving_average_from_data_stream/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/moving_average_from_data_stream/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque


class MovingAverage:
    # Time: O(1) per next call
    # Space: O(size) — window holds at most size values
    def __init__(self, size: int) -> None:
        self.size = size
        self.window: deque[int] = deque()
        self.window_sum = 0

    # Time: O(1)
    # Space: O(1)
    def next(self, val: int) -> float:
        self.window.append(val)
        self.window_sum += val
        if len(self.window) > self.size:
            self.window_sum -= self.window.popleft()
        return self.window_sum / len(self.window)
```

## Complexity

| Time | Space |
| - | - |
| O(1) per next call | O(size) — window holds at most size values |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.