# The Multiplication Trail

> Each step multiplies the whole journey.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list of numbers, return a list of the same length whose i-th element equals the product of nums[0..i] (inclusive).

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **running accumulation with multiplication**, the multiplicative analog of prefix sums. It checks whether candidates can maintain a running product and build the output incrementally.

---

### Break down the requirements

#### Step 1: Initialize a running product

Start with a product of 1 (the multiplicative identity).

#### Step 2: Multiply and append at each step

For each element, multiply it into the running product and append the result to the output list.

#### Step 3: Handle edge cases

An empty list returns an empty list. A list with zeros produces zeros from that point onward.

---

### The solution

**Running product accumulation**

```python
def cumulative_products(nums):
    result = []
    product = 1
    for num in nums:
        product *= num
        result.append(product)
    return result
```

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

> **Interviewers Watch For**
>
> Do you handle the zero case correctly? Once a zero is encountered, the running product stays zero for all subsequent elements. No special handling is needed since multiplication by zero naturally produces this.

> **Common Pitfall**
>
> Initializing the product to 0 instead of 1. Multiplying by 0 gives 0 for every element, producing an all-zero output.

---

## Common follow-up questions

- What if the list contains zeros and you need the product excluding self? _(Tests the classic 'product of array except self' problem using prefix and suffix products.)_
- What if the numbers are very large and overflow is a concern? _(Tests awareness of arbitrary precision in Python vs fixed-width integers in other languages.)_
- How would you compute this in a streaming fashion? _(Tests maintaining the running product as state across calls.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_multiplication_trail)
- [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.