Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

246. Cumulative sum features

easy
GeneralGeneral
senior

Build cumulative-sum (running total) features, a simple but useful feature-engineering trick for time-ordered data. You’ll compute per-group running totals so each row can use information from earlier rows only.

cumsumi=∑j=1ixj\text{cumsum}_{i}=\sum_{j=1}^{i} x_j

Requirements

Implement the function

python

Rules:

  • Treat values as already sorted in time order (do not sort).
  • The cumulative sum should reset when the group id changes (i.e., compute cumsum separately per unique group).
  • Return a NumPy array of floats with the same length as values.
  • Don’t use pandas; use only NumPy and built-in Python libraries.
  • Aim for an efficient approach (single pass is fine).

Example

python

Output:

python
Input Signature
ArgumentType
groupsnp.ndarray
valuesnp.ndarray
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • No pandas; only Python/NumPy.

  • Input order is time order; do not sort.

  • Single pass O(n) expected.

Hint 1

Use a dictionary/map: group_id -> running_sum while scanning left-to-right.

Hint 2

For each index i, read previous sum via totals.get(group, 0.0), add values[i], store back, and write to output.

Hint 3

Single-pass pattern: for i, (v,g) in enumerate(zip(values, groups)): update totals[g] and set out[i] = totals[g].

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
prefix-sum
hashmap
feature-engineering
time-series
20 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit