The Middle Ground
A hard Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- hard
- Seniority
- L5
Problem
Implement a RunningMedian class that supports add(num) and get_median(). get_median returns the current median of all values added so far (for an odd count, the exact middle value; for an even count, the average of the two middle values as a float). Then implement a driver run_operations(operations): it receives a list of operations where each operation is either ['add', x] or ['get_median']. Construct one RunningMedian, apply the operations in order, and return a parallel results list: append None for each 'add' and the returned median for each 'get_median'. Odd-count medians keep the value's original integer type; even-count medians are floats (e.g. 4.0). Values may be negative, zero, or duplicates.
Summary
The middle value keeps moving.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.