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