Bird
Raised Fist0
NumPydata~20 mins

np.cumsum() for cumulative sum in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cumulative Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of np.cumsum() on a 1D array
What is the output of the following code using np.cumsum()?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
cum_sum = np.cumsum(arr)
print(cum_sum)
A[1 3 6 10]
B[1 2 3 4]
C[10 9 7 4]
D[1 4 9 16]
Attempts:
2 left
💡 Hint
Think about adding each element to the sum of all previous elements.
❓ data_output
intermediate
2:00remaining
Cumulative sum along axis in 2D array
What is the output of np.cumsum() when applied along axis 0 on this 2D array?
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
cum_sum = np.cumsum(arr, axis=0)
print(cum_sum)
A
[[1 2]
 [3 4]
 [5 6]]
B
[[1 2]
 [4 6]
 [9 12]]
C
[[1 5 10]
 [2 6 12]]
D
[[1 3]
 [2 4]
 [5 6]]
Attempts:
2 left
💡 Hint
Cumulative sum along axis 0 adds values down each column.
🔧 Debug
advanced
2:00remaining
Identify the error in np.cumsum usage
What error will this code raise?
NumPy
import numpy as np
arr = np.array([1, 2, 3])
cum_sum = np.cumsum(arr, axis=1)
print(cum_sum)
ANo error, outputs [1 3 6]
BValueError: operands could not be broadcast together
CAxisError: axis 1 is out of bounds for array of dimension 1
DTypeError: cumsum() got an unexpected keyword argument 'axis'
Attempts:
2 left
💡 Hint
Check the shape and dimensions of the array and the axis parameter.
🚀 Application
advanced
2:00remaining
Using np.cumsum() to calculate running totals
You have daily sales data: [100, 200, 150, 300]. Which option shows the running total of sales using np.cumsum()?
NumPy
import numpy as np
sales = np.array([100, 200, 150, 300])
running_total = np.cumsum(sales)
print(running_total)
A[100 300 350 650]
B[100 200 150 300]
C[100 250 400 700]
D[100 300 450 750]
Attempts:
2 left
💡 Hint
Add each day's sales to the sum of all previous days.
🧠 Conceptual
expert
2:00remaining
Effect of np.cumsum() with different data types
Given arr = np.array([1, 2, 3], dtype='int8'), what is the output of np.cumsum(arr)?
NumPy
import numpy as np
arr = np.array([1, 2, 3], dtype='int8')
cum_sum = np.cumsum(arr)
print(cum_sum)
A[1 3 6]
B[1 3 -127]
C[1 3 5]
D[1 2 3]
Attempts:
2 left
💡 Hint
Consider how cumulative sum works and the range of int8.

Practice

(1/5)
1. What does the np.cumsum() function do when applied to a 1D numpy array?
easy
A. Calculates the cumulative sum of the elements step by step
B. Calculates the product of all elements
C. Sorts the array in ascending order
D. Finds the maximum value in the array

Solution

  1. Step 1: Understand the function purpose

    np.cumsum() adds elements cumulatively, meaning each element is the sum of all previous elements plus itself.
  2. Step 2: Compare with other options

    Options B, C, and D describe different operations: product, sorting, and max value, which are not what np.cumsum() does.
  3. Final Answer:

    Calculates the cumulative sum of the elements step by step -> Option A
  4. Quick Check:

    np.cumsum() = cumulative sum [OK]
Hint: Remember: cumsum means running total sum [OK]
Common Mistakes:
  • Confusing cumulative sum with product
  • Thinking it sorts the array
  • Assuming it finds max or min values
2. Which of the following is the correct syntax to compute the cumulative sum of a numpy array arr?
easy
A. np.cumsum(arr)
B. All of the above
C. np.cumsum(arr, axis=0)
D. arr.cumsum()

Solution

  1. Step 1: Check numpy function usage

    np.cumsum(arr) is the basic syntax to get cumulative sum of all elements in the array.
  2. Step 2: Check array method and axis parameter

    arr.cumsum() is a valid method call on numpy arrays. Also, np.cumsum(arr, axis=0) computes cumulative sum along the first axis, valid for multi-dimensional arrays.
  3. Final Answer:

    np.cumsum(arr) -> Option A
  4. Quick Check:

    Basic syntax is np.cumsum(arr) [OK]
Hint: np and array method both work; axis optional [OK]
Common Mistakes:
  • Thinking only np.cumsum(arr) works
  • Ignoring axis parameter for multi-dimensional arrays
  • Assuming cumsum is not an array method
3. What is the output of the following code?
import numpy as np
arr = np.array([2, 4, 6, 8])
cum_sum = np.cumsum(arr)
print(cum_sum)
medium
A. [2 8 14 22]
B. [2 4 6 8]
C. [2 6 12 20]
D. [20 18 14 8]

Solution

  1. Step 1: Calculate cumulative sums stepwise

    Start with 2, then 2+4=6, then 6+6=12, then 12+8=20.
  2. Step 2: Form the cumulative sum array

    The resulting array is [2, 6, 12, 20].
  3. Final Answer:

    [2 6 12 20] -> Option C
  4. Quick Check:

    Running total sums = [2, 6, 12, 20] [OK]
Hint: Add each element to sum of all before it [OK]
Common Mistakes:
  • Confusing cumulative sum with original array
  • Adding elements incorrectly
  • Reversing the order of sums
4. The following code throws an error. What is the problem?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
cum_sum = np.cumsum(arr, axis=2)
print(cum_sum)
medium
A. Syntax error in np.cumsum call
B. np.cumsum does not accept axis parameter
C. Array must be 1D for cumsum
D. Axis 2 does not exist for a 2D array

Solution

  1. Step 1: Check array dimensions

    The array has shape (2, 2), so it has axes 0 and 1 only.
  2. Step 2: Validate axis parameter

    Using axis=2 is invalid because axis 2 does not exist for this 2D array, causing an error.
  3. Final Answer:

    Axis 2 does not exist for a 2D array -> Option D
  4. Quick Check:

    Axis must be within array dimensions [OK]
Hint: Axis must be less than array dimensions [OK]
Common Mistakes:
  • Assuming axis parameter is optional but invalid
  • Thinking cumsum only works on 1D arrays
  • Confusing syntax error with axis error
5. Given a 2D numpy array representing daily sales for 3 products over 4 days:
sales = np.array([[5, 3, 2],
                  [4, 6, 1],
                  [7, 2, 3],
                  [3, 5, 4]])

Which code correctly computes the cumulative sales per product over the days?
hard
A. np.cumsum(sales)
B. np.cumsum(sales, axis=0)
C. np.cumsum(sales, axis=1)
D. np.cumsum(sales, axis=2)

Solution

  1. Step 1: Understand the array shape and axis meaning

    The array shape is (4, 3): 4 days (rows), 3 products (columns). Axis 0 moves down rows (days), axis 1 moves across products.
  2. Step 2: Choose axis for cumulative sum per product over days

    To get cumulative sales per product over days, sum down each column (axis=0).
  3. Final Answer:

    np.cumsum(sales, axis=0) -> Option B
  4. Quick Check:

    Cumulative sum down rows = axis 0 [OK]
Hint: Sum down rows (axis=0) for cumulative per product [OK]
Common Mistakes:
  • Using axis=1 sums across products, not over days
  • Ignoring axis parameter
  • Using invalid axis=2 for 2D array
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard