Maintain a Fixed-Window Rolling Mean in Constant Time
Company: Jump Trading
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: Implement `rolling_means(values, window)` for signed integer values, returning a double-precision mean for every complete consecutive window. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.
Constraints
- 0 <= len(values) <= 200000.
- Each value is an integer from -10^9 through 10^9.
- Return an empty list if window <= 0 or window > len(values).
- A signed 64-bit running total is sufficient; results are compared with absolute or relative tolerance 1e-9.
Examples
Input: ([], 1)
Expected Output: []
Explanation: A positive window exceeds an empty input and returns an empty list.
Input: ([], 0)
Expected Output: []
Explanation: A zero window is invalid even for empty input.
Hints
- Test a one-element window, a full-array window, and both zero and oversized windows.
- Include negative values, exact cancellation, and values at both numeric limits.
- Use an input with several overlapping windows whose means are not all integers.