Skip to main content

80. Product of Array Except Self

mediumAsked at Reddit

Short answer

  • Best approach: Two-pass prefix/suffix. First pass: out[i] = product of nums[0..i-1]. Second pass: multiply by product of nums[i+1..n].
  • Time O(n), space O(1) extra (output not counted)
  • How often Reddit asks it: frequently asked · Verified

Return an array where each element is the product of all others (no division). Reddit uses this to test prefix/suffix product technique — the same shape used in their cross-shard counter reconciliation where you compute totals excluding one shard.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)— Reddit data-platform on-site question.
  • Blind (2025-11)— Reported on Reddit infra rounds.

Problem

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation.

Constraints

  • 2 <= nums.length <= 10^5
  • -30 <= nums[i] <= 30
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

Examples

Example 1

Input
nums = [1,2,3,4]
Output
[24,12,8,6]

Example 2

Input
nums = [-1,1,0,-3,3]
Output
[0,0,9,0,0]

Approaches

1. Total product / each element (banned)

Compute total product; for each i, total / nums[i].

Time
O(n)
Space
O(1)
// Banned by the problem (no division). Also fails when nums[i] === 0.

Tradeoff: Doesn't satisfy the constraint.

2. Two-pass prefix/suffix (optimal)

First pass: out[i] = product of nums[0..i-1]. Second pass: multiply by product of nums[i+1..n].

Time
O(n)
Space
O(1) extra (output not counted)
function productExceptSelf(nums) {
  const n = nums.length;
  const out = new Array(n);
  out[0] = 1;
  for (let i = 1; i < n; i++) out[i] = out[i - 1] * nums[i - 1];
  let suffix = 1;
  for (let i = n - 1; i >= 0; i--) {
    out[i] *= suffix;
    suffix *= nums[i];
  }
  return out;
}

Tradeoff: Linear, O(1) extra. The two-pass prefix/suffix is the canonical trick.

Reddit-specific tips

Reddit interviewers want O(1) extra space using the output array itself. Bonus signal: explain that division would fail on zeros — your algorithm naturally handles them.

Common mistakes

  • Using division (explicitly forbidden).
  • Allocating two extra arrays (prefix and suffix) when output array suffices.
  • Forgetting the suffix initialization (start at 1).

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • What if division IS allowed? (Handle zero count.)
  • Maximum product subarray (LC 152).
  • Sum except self (variant).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Ctrl+Enter to execute

FAQ

Why store prefix in the output then multiply by suffix?

The output array becomes our scratch space. After the prefix pass, out[i] = prefix product. The second pass overlays suffix.

Why does division fail with zeros?

If exactly one zero, only that index has non-zero answer. If multiple zeros, all answers are zero. Division can't divide by zero.

Originally published on InterviewChamp.AI

Companies that also ask Product of Array Except Self