Coding interview problems › Arrays & Hashing
#️⃣ Arrays & Hashing Medium
Product of Array Except Self
Product of Array Except Self explained: build prefix and suffix products in two passes, no division, O(n) time. Tested Python, zero cases, live visual.
Also searched as: product of array except self without division · prefix and suffix products
Try it live Free
Prefix Sums & Kadane
Watch prefix sums turn any range question into a running value carried left to right. This problem uses the same idea with multiplication, once from the left and once from the right.
The problem
Given a list of integers nums, return a list out where out[i] is the product of every element except nums[i]. Do it in O(n) time without using division.
Only the zero's own position gets a non-zero product.
The key insight
The product of everything except nums[i] is (product of everything to the left of i) times (product of everything to the right of i). Both are prefix-style running values, the multiplication version of prefix sums, so two passes give every answer.
Step-by-step approach
- Create
outof length n filled with 1. - Left pass: keep
left= product of elements before i. Setout[i] = left, then multiplyleftbynums[i]. - Right pass from the end: keep
right= product of elements after i. Multiplyout[i]byright, then multiplyrightbynums[i]. - Return
out; it now holds left-product times right-product at every index.
Python solution
def product_except_self(nums):
n = len(nums)
out = [1] * n
left = 1
for i in range(n): # out[i] = product of nums[:i]
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1): # multiply in product of nums[i+1:]
out[i] *= right
right *= nums[i]
return outComplexity
Two linear passes. Apart from the output list, which the problem requires, only two running products are stored.
Common mistakes
- Using total product divided by
nums[i], which crashes or gives wrong answers when the list contains a zero. - Updating
leftbefore writingout[i], which includesnums[i]in its own product. - Allocating separate prefix and suffix arrays is fine, but forgetting that the follow-up often asks for O(1) extra space.
Frequently asked questions
What is the time and space complexity of Product of Array Except Self?
O(n) time with two passes, and O(1) extra space if the output array does not count. With separate prefix and suffix arrays it is O(n) extra space.
Why not just divide the total product?
Division fails when an element is zero, and the problem forbids it. The prefix and suffix approach handles any number of zeros correctly.
What happens with two zeros?
Every output is 0, because every index's left or right side contains at least one zero. The two-pass method gets this automatically.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Arrays & Hashing Hash Tables lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.