Product of Array Except Self
A medium Arrays interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Arrays
- Sheets
- 3
- Core for
- 26 roles
The problem
Given an integer array, construct a new array where each element at index i equals the product of all elements in the original array except the one at i, without using division.
Example 1
- Input
- nums = [2,3,4,5]
- Output
- [60,40,30,24]
- Why
- Index 0 gets 3*4*5 = 60, index 1 gets 2*4*5 = 40, index 2 gets 2*3*5 = 30 and index 3 gets 2*3*4 = 24.
Example 2
- Input
- nums = [-2,1,0,3,-1]
- Output
- [0,0,6,0,0]
- Why
- Every product that includes the 0 is 0; only the position of the 0 multiplies the other four values: (-2) * 1 * 3 * (-1) = 6.
Example 3
- Input
- nums = [2,3]
- Output
- [3,2]
- Why
- Each position receives the only remaining other element.
Constraints
- 2 <= nums.length <= 10^5
- -30 <= nums[i] <= 30
How to think about it
Updated 2026-09-29The product of everything except index i splits cleanly into two independent pieces: the prefix product of everything to its left, and the suffix product of everything to its right. Computing running prefix products left-to-right and running suffix products right-to-left assembles the answer for every cell without ever needing arithmetic division.
Approaches, worst first
Brute force exclusion product
time O(n^2) · space O(1)
For each index i, run a nested loop over all indices j != i and multiply them together. Follows the exact specification without allocating extra arrays, but takes quadratic time and scales poorly on large inputs.
Prefix and suffix arrays
time O(n) · space O(n)
Build prefix[i] holding the product of all numbers before i, and suffix[i] holding the product of all numbers after i. Multiply prefix[i] and suffix[i] to populate the result. Linear time, but requires two additional helper arrays.
Single output array two-pass accumulationWrite this one
time O(n) · space O(1)
Fill the output array with prefix products in a forward sweep. Then sweep backwards with a single running integer tracking the suffix product, multiplying it into the corresponding output slot on the fly. Achieves linear time using zero extra memory beyond the output.
Where people lose marks · 3
- Using division by the total product, which crashes on zeros and violates the explicit constraint against using division.
- Incorrectly handling multiple zeros, where every output entry must be 0 rather than reporting non-zero values.
- Initializing prefix or suffix accumulators to 0 instead of 1, resulting in all-zero output arrays.
Full solution
Single output array two-pass accumulation: a forward sweep fills prefix products into the answer, then a backward sweep multiplies in a running suffix product. Linear time, no division, and no extra array beyond the output.
Python
from typing import List
def product_except_self(nums: List[int]) -> List[int]:
n = len(nums)
answer = [1] * n
prefix = 1
for i in range(n):
answer[i] = prefix
prefix *= nums[i]
suffix = 1
for i in range(n - 1, -1, -1):
answer[i] *= suffix
suffix *= nums[i]
return answer
JavaScript
function productExceptSelf(nums) {
const n = nums.length;
const answer = new Array(n).fill(1);
let prefix = 1;
for (let i = 0; i < n; i++) {
answer[i] = prefix;
prefix *= nums[i];
}
let suffix = 1;
for (let i = n - 1; i >= 0; i--) {
answer[i] *= suffix;
suffix *= nums[i];
}
return answer.map((v) => (v === 0 ? 0 : v)); // normalize -0 to 0
}
The theory behind it
Arrays — the ground this problem stands on. All Arrays problems
What Arrays is
An array is a row of fixed boxes laid side by side in computer memory, like numbered lockers in a hallway. Because each box occupies identical space and sits directly next to its neighbors, jumping to locker zero or locker ten thousand takes the exact same tiny fraction of time. Every box holds an item of the same type, addressed by an offset number called an index.
When to reach for it
Reach for an array when items arrive in a known sequence and need immediate retrieval by position number. Problems asking for running totals, prefix accumulations, cyclic rotations, or in-place rearrangements signal array mechanics. Whenever constraints require constant-time random lookups or contiguous cache scans across fixed collections, a flat sequence is the default container.
How the pattern works
Visualize a tape with zero-indexed slots stretching from start to end. Keep track of write and read cursors when modifying contents without allocating helper buffers. For running computations, maintain an invariant such as having processed all elements left of the current index while pending elements wait to the right. When modifying entries in place, consider scanning backwards from the end so unread data is not overwritten.
More Arrays problems
Target Roles
Core requirement for 26 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .