Compute a moving average of a NumPy array in linear time with np.cumsum

Quick Overview

Compute a moving average over a 1-D NumPy array in linear time using cumulative sums, with no Python loop. It tests prefix-sum indexing, off-by-one handling at the first window, input validation, and floating-point error in long running sums.

Compute a moving average of a NumPy array in linear time with np.cumsum

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a 1-D array `a` and a window size `n`, return the moving average as an array of length `len(a) - n + 1`, where element `i` is the mean of `a[i:i+n]`. The function must meet these requirements: - Compute the window sums with `np.cumsum` in $O(\text{len}(a))$ time: no Python loop and no $O(n)$ work per window. - Output values are floats (window sums divided by `n`). ```python a = np.array([1, 2, 3, 4, 5]) moving_average(a, 3) # -> array([2., 3., 4.]) ``` The three windows are `[1, 2, 3]`, `[2, 3, 4]` and `[3, 4, 5]`, with means 2, 3 and 4. ```hint Two running totals Write the sum of `a[i:i+n]` using the running total up to the end of the window and the running total just before it starts. Check that your formula also works for the first window. ``` ### Clarifying Questions to Ask - What should happen when `n` is larger than `len(a)`, or when `n` is zero or negative? - Can `a` hold integers large enough that a running sum overflows, or NaN and infinite values? ### What a Strong Answer Covers - Expressing each window sum as the difference of two prefix sums, with the indexing right for the first window. - Returning exactly `len(a) - n + 1` float values. - Linear time and the extra memory the prefix sums need. - Validating the window size. - Numerical problems that come from long running sums. ### Follow-up Questions - Why prepend a 0 to the `cumsum` before differencing? - How does floating-point error accumulate with `cumsum` on large arrays, and what can you do about it? - How would you compute the same moving average along each row of a 2-D array? - How would you return a trailing average with the same length as `a`, averaging over the available elements for the first `n - 1` positions?

Overview: Compute a moving average over a 1-D NumPy array in linear time using cumulative sums, with no Python loop. It tests prefix-sum indexing, off-by-one handling at the first window, input validation, and floating-point error in long running sums.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Given a 1-D array a and a window size n, return the moving average as an array of length len(a) - n + 1, where element i is the mean of a[i:i+n].

The function must meet these requirements:

  • Compute the window sums with np.cumsum in O(len(a))O(\text{len}(a)) time: no Python loop and no O(n)O(n) work per window.
  • Output values are floats (window sums divided by n ).
a = np.array([1, 2, 3, 4, 5])
moving_average(a, 3)  # -> array([2., 3., 4.])

The three windows are [1, 2, 3], [2, 3, 4] and [3, 4, 5], with means 2, 3 and 4.

Clarifying Questions to Ask Guidance

  • What should happen when n is larger than len(a) , or when n is zero or negative?
  • Can a hold integers large enough that a running sum overflows, or NaN and infinite values?

What a Strong Answer Covers Guidance

  • Expressing each window sum as the difference of two prefix sums, with the indexing right for the first window.
  • Returning exactly len(a) - n + 1 float values.
  • Linear time and the extra memory the prefix sums need.
  • Validating the window size.
  • Numerical problems that come from long running sums.

Follow-up Questions Guidance

  • Why prepend a 0 to the cumsum before differencing?
  • How does floating-point error accumulate with cumsum on large arrays, and what can you do about it?
  • How would you compute the same moving average along each row of a 2-D array?
  • How would you return a trailing average with the same length as a , averaging over the available elements for the first n - 1 positions?