Skip to main content

19. Product of Array Except Self

mediumAsked at Intuit

Given an array, return an array where each element is the product of all other elements — without using division and in O(n) time. Intuit asks this to test prefix/suffix patterns and to probe whether you avoid division (which fails on zero) in financial calculations.

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

Source citations

Public interview reports confirming this problem appears in Intuit loops.

  • Glassdoor (2026-Q1)Intuit SWE II onsite — prefix/suffix product round, division explicitly disallowed.
  • LeetCode Discuss (2025-09)QuickBooks interviewer pressed for O(1) extra space (output array doesn't count).

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
  • Product of any prefix or suffix fits in 32-bit signed 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 divided by self

Compute the product of everything; for each index, divide by nums[i]. Fails on zeros and division is disallowed.

Time
O(n)
Space
O(1)
function productExceptSelf(nums) {
  const total = nums.reduce((a, b) => a * b, 1);
  return nums.map(n => total / n); // BROKEN on zeros and disallowed
}

Tradeoff: Fails on zeros; division is explicitly disallowed. Mention but reject.

2. Two-pass prefix + suffix product (optimal)

First pass: answer[i] = product of all elements to the left. Second pass (right-to-left): multiply by product of all elements to the right. O(1) extra space if you reuse the output array.

Time
O(n)
Space
O(1) extra (output excluded)
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 right = 1;
  for (let i = n - 1; i >= 0; i--) {
    out[i] *= right;
    right *= nums[i];
  }
  return out;
}

Tradeoff: Two linear passes, single auxiliary scalar. Handles zeros correctly because we never divide.

Intuit-specific tips

Intuit asks this because their financial calculation engines never use division on raw money values — division introduces rounding errors. They grade for whether you recognize the prefix/suffix pattern in 5 minutes. Bonus signal: discuss how this generalizes to range-product queries (segment tree) and why integer overflow matters when scaling to ledger totals.

Common mistakes

  • Reaching for division and missing the explicit constraint.
  • Allocating two separate prefix/suffix arrays — fails the O(1) extra space goal.
  • Forgetting that prefix[0] and suffix[n-1] should be 1 (the empty product).

Follow-up questions

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

  • Support negative-value tolerance with integer cents — does the algorithm change?
  • Range product queries — given updates, answer product(L, R) — segment tree extension.
  • What if integer overflow is possible? Use BigInt or modular arithmetic.

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 is division disallowed?

Two reasons: (1) division by zero breaks the formula if any element is 0; (2) Intuit treats this as a stand-in for financial math where division on raw amounts introduces rounding error. Use prefix/suffix multiplication instead.

Does the output array count toward space complexity?

By convention, the output is excluded from auxiliary space analysis. So the prefix/suffix approach is O(1) extra space.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →