# The Running Total

> Each position holds the sum of everything before it.

Canonical URL: <https://datadriven.io/problems/the_running_total>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list of integers, return a list where position i is the sum of input[0..i] inclusive.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests the **prefix sum pattern**, the most fundamental cumulative operation in data processing. It checks whether candidates can build a running accumulator in a single pass.

---

### Break down the requirements

#### Step 1: Initialize a running sum

Start at 0.

#### Step 2: Add each element and store the result

For each element, add it to the running sum and append the sum to the output.

---

### The solution

**Single-pass prefix sum accumulation**

```python
def running_total(nums):
    result = []
    total = 0
    for num in nums:
        total += num
        result.append(total)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) with a single pass.
> 
> **Space:** O(n) for the output list.

> **Interviewers Watch For**
>
> This is the simplest possible accumulation pattern. Interviewers check whether you implement it cleanly without unnecessary complexity. Getting this wrong signals fundamental gaps.

> **Common Pitfall**
>
> Using `sum(nums[:i+1])` inside the loop. This recomputes the sum from scratch each iteration, turning O(n) into O(n^2).

---

## Common follow-up questions

- How would you compute the sum of any subarray in O(1) using prefix sums? _(Tests `prefix[right] - prefix[left - 1]` for range sum queries.)_
- What if you needed a running product instead? _(Tests replacing addition with multiplication and handling zeros.)_
- How does this relate to SQL window functions? _(Tests `SUM(col) OVER (ORDER BY id)` as the SQL equivalent.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_running_total)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.