Skip to main content

238. Product of Array Except Self

mediumAsked at Gusto

Compute, for each index, the product of all other elements — without using division. Gusto asks this to test whether candidates see the prefix-suffix decomposition and can then optimise it to O(1) extra space by computing the suffix product on the fly.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Gusto loops.

  • Glassdoor (2026-Q1)Reported as a medium problem in Gusto onsite sessions, frequently with a 'no division' constraint called out explicitly.
  • Blind (2025-10)Gusto interview threads list Product of Array Except Self as high-signal for prefix/suffix thinking.

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]

Explanation: answer[0] = 2×3×4 = 24, answer[1] = 1×3×4 = 12, answer[2] = 1×2×4 = 8, answer[3] = 1×2×3 = 6.

Example 2

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

Explanation: The zero at index 2 makes most products zero; only index 2 survives (product of −1×1×−3×3 = 9).

Approaches

1. Prefix + suffix arrays (O(n) space)

Compute prefix products left to right and suffix products right to left. answer[i] = prefix[i-1] * suffix[i+1].

Time
O(n)
Space
O(n)
function productExceptSelf(nums) {
  const n = nums.length;
  const prefix = new Array(n).fill(1);
  const suffix = new Array(n).fill(1);
  for (let i = 1; i < n; i++) prefix[i] = prefix[i - 1] * nums[i - 1];
  for (let i = n - 2; i >= 0; i--) suffix[i] = suffix[i + 1] * nums[i + 1];
  return prefix.map((p, i) => p * suffix[i]);
}

Tradeoff: Clean and easy to verify. Good starting point. The output array itself is not counted as extra space per the problem convention, so this is O(n) extra space for the two auxiliary arrays.

2. O(1) extra space (running suffix)

Use the output array to hold prefix products (left pass). In a right pass, maintain a running suffix product and multiply it into each output element in-place.

Time
O(n)
Space
O(1) extra (output array not counted)
function productExceptSelf(nums) {
  const n = nums.length;
  const answer = new Array(n).fill(1);
  // left pass: answer[i] = product of nums[0..i-1]
  for (let i = 1; i < n; i++) {
    answer[i] = answer[i - 1] * nums[i - 1];
  }
  // right pass: multiply by product of nums[i+1..n-1]
  let rightProduct = 1;
  for (let i = n - 1; i >= 0; i--) {
    answer[i] *= rightProduct;
    rightProduct *= nums[i];
  }
  return answer;
}

Tradeoff: Optimal: O(n) time, O(1) extra space. The running rightProduct variable replaces the suffix array. This is the expected final solution at Gusto.

Gusto-specific tips

Narrate the insight: 'For each index i, the answer is the product of everything to the left times the product of everything to the right. I can compute both sides in two passes.' Start with the two-array version, then note that the suffix array can be replaced by a single running variable, reducing extra space to O(1). Gusto appreciates this stepwise optimisation narration.

Common mistakes

  • Using division (total product / nums[i]) — the problem explicitly forbids it, and it fails when nums contains a zero.
  • Off-by-one in the prefix/suffix arrays — prefix[i] should be the product of elements before i, not including i.
  • Forgetting to initialise prefix[0] and suffix[n-1] to 1 — these represent empty products.
  • Handling zeros incorrectly — the prefix/suffix approach handles zeros naturally; no special-casing needed.

Follow-up questions

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

  • What if the input can contain zeros — does your solution still work? (Yes — the prefix/suffix approach handles it.)
  • Extend to a 2D matrix: for each cell, product of all elements except those in the same row or column.
  • How would you test edge cases like a two-element array?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why does the problem ban division?

Division fails when an element is zero (division by zero). It also introduces floating-point issues. The prefix-suffix approach avoids both problems.

Is the output array counted as extra space?

Per the problem's convention, the output array is not counted. The O(1) claim refers to auxiliary space beyond input and output.

What does the answer look like for all-zeros input?

answer[i] is 0 for all i, since the product of all other elements (which includes at least one zero) is always zero.

Practice these live with InterviewChamp.AI

Drill Product of Array Except Self and other Gusto interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →