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.

Open the playground →

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.

Inputnums = [1, 2, 3, 4]
Output[24, 12, 8, 6]
Inputnums = [-1, 1, 0, -3, 3]
Output[0, 0, 9, 0, 0]

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.

Brute force first: Recomputing the product of the other n-1 elements for every index is O(n²); dividing the total product by nums[i] breaks on zeros and is not allowed.

Step-by-step approach

  1. Create out of length n filled with 1.
  2. Left pass: keep left = product of elements before i. Set out[i] = left, then multiply left by nums[i].
  3. Right pass from the end: keep right = product of elements after i. Multiply out[i] by right, then multiply right by nums[i].
  4. Return out; it now holds left-product times right-product at every index.

Python solution

PYTHON 3tested
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 out

Complexity

TimeO(n)
SpaceO(1) extra

Two linear passes. Apart from the output list, which the problem requires, only two running products are stored.

Common mistakes

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.