Where the Numbers Settle
A easy Python interview practice problem on DataDriven. Write and execute real python code with instant feedback.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L5
Problem
Sensor readings arrive one at a time, and a dashboard has to show the running mean for each sensor at any moment. Implement a `StreamAverager` class: `add(key, value)` records one numeric reading under `key`, and `get_averages()` returns a dict mapping each key seen so far to the mean of its readings, or an empty dict before anything is recorded. A driver `run_stream_averager(operations)` is provided: `operations` is a list where each entry is either `['add', key, value]` or `['get_averages']`. Build one `StreamAverager`, apply the operations in order, and return a parallel list of results, with `None` for each `'add'` and the dict for each `'get_averages'`. Means are floats, so `add('sensor_a', 10)`, `add('sensor_a', 30)`, `get_averages()` gives `{'sensor_a': 20.0}`.
Summary
The answer moves with the data.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and checks it automatically.