The Multiplication Trail

An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
easy
Seniority
L3
Asked in
Citadel

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).

Each step multiplies the whole journey.

Example 1

Input:

nums = [1,2,3,4]

Output:

[1,2,6,24]

Example 2

Input:

nums = [2,3,4]

Output:

[2,6,24]

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
def cumulative_products(nums):
    result = []
    product = 1
    for num in nums:
        product *= num
        result.append(product)
    return result

Common follow-up questions