# The Blind Multiplier

> Compute the result of everything around you - without seeing yourself.

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

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given a list of integers, return a list of the same length where position i is the product of all input elements except nums[i]. Do not use division. Runtime should be O(n).

## Worked solution and explanation

### Why this problem exists in real interviews

Product of array except self without division tests **prefix and suffix product** computation. It is a classic array problem that checks whether you can decompose the problem into two passes.

> **Trick to Solving**
>
> Build a prefix product array (product of all elements to the left) and a suffix product array (product of all elements to the right). The answer at each index is `prefix[i] * suffix[i]`. This avoids division entirely.

---

### Break down the requirements

#### Step 1: Compute prefix products

For each index i, `prefix[i]` is the product of all elements before i.

#### Step 2: Compute suffix products

For each index i, `suffix[i]` is the product of all elements after i.

#### Step 3: Multiply prefix and suffix at each position

The product of everything except `nums[i]` is `prefix[i] * suffix[i]`.

---

### The solution

**Prefix-suffix product decomposition**

```python
def product_except_self(nums):
    n = len(nums)
    result = [1] * n
    prefix = 1
    for i in range(n):
        result[i] = prefix
        prefix *= nums[i]
    suffix = 1
    for i in range(n - 1, -1, -1):
        result[i] *= suffix
        suffix *= nums[i]
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) with two linear passes.
> 
> **Space:** O(1) extra beyond the output array. The prefix and suffix are computed using running variables.

> **Interviewers Watch For**
>
> The O(1) extra space optimization: using the result array for prefix products and a single variable for the suffix pass. This avoids allocating separate prefix and suffix arrays.

> **Common Pitfall**
>
> Using division: `total_product / nums[i]`. This fails when any element is 0 and is explicitly prohibited by the problem.

---

## Common follow-up questions

- What if the array contains zeros? _(Tests that the prefix-suffix approach handles zeros naturally, unlike the division approach.)_
- Can you solve this with O(1) extra space? _(Tests the two-pass approach using the output array as shown in the solution.)_
- What if you needed the result modulo a prime? _(Tests applying modulo at each multiplication step to prevent overflow (relevant in languages with fixed-width integers).)_

## Related

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