The Stream Averager
A easy Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L5
Problem
Implement the StreamAverager class with add(key, value) and get_averages(). add(key, value) records one numeric reading for key. get_averages() returns a dict mapping each key seen so far to the average (mean) of its values; before any reading is added it returns an empty dict {}. A driver run_stream_averager(operations) is provided: operations is a list where each entry is either ['add', key, value] or ['get_averages']. Create a single StreamAverager, apply the operations in order on it, and return a parallel list of results: None for each 'add' and the dict for each 'get_averages'. Averages are floats (e.g. add('sensor_a', 10), add('sensor_a', 30), get_averages() -> {'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 grades it automatically.