How do you calculate the cumulative sum or product of a NumPy array?

Quick Overview

Asks how to compute a running sum or running product of a NumPy array and how the result behaves on 2D input. Tests knowledge of the axis argument, the default flattening, NaN handling and integer overflow in long cumulative products.

How do you calculate the cumulative sum or product of a NumPy array?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How do you calculate the cumulative sum or product of a NumPy array? Explain which functions you would use, what they return, and how they behave on a 2D array. ```hint Keep every intermediate value A plain sum collapses the array to one number, and here you want each running total kept. Once you have that, check what happens on a 2D array when you do not pass an axis. ``` ### What a Strong Answer Covers - The functions for a running sum and a running product and the shape of what they return - How the axis argument changes the result on a 2D array, including the default - Pitfalls such as NaN values and integer overflow in long products ### Follow-up Questions - How would you compute a cumulative sum from the end of the array backwards? - What happens when you call `np.cumprod` on a long integer array, and how would you avoid it? - How would you compute a running total per group, for example per user, in NumPy or pandas?

Overview: Asks how to compute a running sum or running product of a NumPy array and how the result behaves on 2D input. Tests knowledge of the axis argument, the default flattening, NaN handling and integer overflow in long cumulative products.

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

How do you calculate the cumulative sum or product of a NumPy array?

Explain which functions you would use, what they return, and how they behave on a 2D array.

What a Strong Answer Covers Guidance

  • The functions for a running sum and a running product and the shape of what they return
  • How the axis argument changes the result on a 2D array, including the default
  • Pitfalls such as NaN values and integer overflow in long products

Follow-up Questions Guidance

  • How would you compute a cumulative sum from the end of the array backwards?
  • What happens when you call np.cumprod on a long integer array, and how would you avoid it?
  • How would you compute a running total per group, for example per user, in NumPy or pandas?