Quick Overview

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.

Maintain a Fixed-Window Rolling Mean in Constant Time

Company: Jump Trading

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Maintain a Fixed-Window Rolling Mean in Constant Time Implement `rolling_means(values, window)` for signed integer values, returning a double-precision mean for every complete consecutive window. Return an empty list if `window <= 0` or exceeds the input length. Each update after the first window must use constant time. The judge compares each returned value with absolute or relative tolerance `1e-9`. Constraints: up to `200000` values; each value is in `[-10^9, 10^9]`; a signed 64-bit running total is sufficient for every valid window. ```hint Check window edges Include a one-element window, a full-array window, negative values, and an invalid window size. ```

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.

Given signed integer `values` and `window`, return one double-precision mean for every complete consecutive window. Return an empty list when `window <= 0` or when it exceeds the input length. After the first window, each update must take constant time. The judge uses absolute or relative tolerance `1e-9` for every returned value.

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

  1. Test a one-element window, a full-array window, and both zero and oversized windows.
  2. Include negative values, exact cancellation, and values at both numeric limits.
  3. Use an input with several overlapping windows whose means are not all integers.

Loading coding console...