Skip to main content

238. Product of Array Except Self

mediumAsked at IBM

Product of Array Except Self is IBM's prefix/suffix-array screener. The interviewer is grading whether you avoid the forbidden division operator, derive the two-pass left/right product technique, and ship it in O(1) extra space using the output array as your scratch.

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

Source citations

Public interview reports confirming this problem appears in IBM loops.

  • Glassdoor (2026-Q1)IBM SWE-2 / SWE-3 recurring onsite problem.
  • LeetCode (2026-Q1)Top-30 IBM-tagged problem (medium tier).
  • GeeksforGeeks (2025-12)Listed in IBM interview-experience archive.

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 divided by self (FORBIDDEN by problem)

Compute total product; divide each element. Fails when any element is 0.

Time
O(n)
Space
O(1)
function productExceptSelfDivision(nums) {
  // Disallowed by the problem; included only to discuss
  let total = 1;
  let zeroCount = 0;
  for (const n of nums) {
    if (n === 0) zeroCount++;
    else total *= n;
  }
  return nums.map((n) => {
    if (zeroCount > 1) return 0;
    if (zeroCount === 1) return n === 0 ? total : 0;
    return total / n;
  });
}

Tradeoff: Explicitly disallowed by the problem statement. Mention it to dismiss it — using division earns an automatic 'did not read the spec' deduction at IBM.

2. Two arrays — left and right products

left[i] = product of all elements before i; right[i] = product of all after. Answer is left[i] * right[i].

Time
O(n)
Space
O(n)
function productExceptSelfTwoArrays(nums) {
  const n = nums.length;
  const left = new Array(n);
  const right = new Array(n);
  left[0] = 1;
  for (let i = 1; i < n; i++) left[i] = left[i - 1] * nums[i - 1];
  right[n - 1] = 1;
  for (let i = n - 2; i >= 0; i--) right[i] = right[i + 1] * nums[i + 1];
  const out = new Array(n);
  for (let i = 0; i < n; i++) out[i] = left[i] * right[i];
  return out;
}

Tradeoff: Conceptually clear and ships in 8 lines. O(n) time but O(n) extra space — fails the 'O(1) extra space' follow-up which IBM almost always asks. Useful as the bridge step on the whiteboard.

3. Output array as scratch (optimal O(1) extra)

First pass fills the output with left products. Second pass walks right-to-left, multiplying by a running right product.

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 rightProduct = 1;
  for (let i = n - 1; i >= 0; i--) {
    out[i] *= rightProduct;
    rightProduct *= nums[i];
  }
  return out;
}

Tradeoff: O(n) time, O(1) extra space. The output array carries the left-products through the first pass, then the second pass multiplies in the right-products on the fly. This is the canonical IBM answer.

IBM-specific tips

IBM grades two specific moves on this problem: (1) you do NOT use division — the problem explicitly forbids it, and 'forgot to read the spec' is a common rubric flag, (2) you ship the O(1)-extra-space version. The intermediate two-array version is a fine whiteboard derivation step but a candidate who stops there loses the senior-bar checkbox. Always finish with the output-as-scratch variant.

Common mistakes

  • Using division — the problem explicitly forbids it and zero values break it.
  • Initializing out[0] = nums[0] instead of out[0] = 1 — wrong by a factor of nums[0].
  • Forgetting that the output array doesn't count toward space complexity but a separate scratch array does.
  • Computing the right product BEFORE multiplying it into out[i] — off-by-one (you must multiply first, then update).
  • Off-by-one on the second pass loop bound — must include index 0.

Follow-up questions

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

  • What if you're ALLOWED to use division — handle the zero edge case correctly.
  • What if the array contains only positive integers — does the algorithm change?
  • Maximum Product Subarray (LeetCode 152) — sign-tracking DP variant.
  • What if you needed the product of every prefix instead?

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 IBM forbid division?

Two reasons: (1) division fails when any element is 0 — you'd have to special-case, (2) the technique generalizes to non-invertible operations (e.g., bitwise XOR, matrix multiplication of non-square matrices). The prefix/suffix pattern works for any associative operation; division is a shortcut that doesn't generalize.

Is the output array O(n) space?

Yes, but problems like this conventionally don't count the output array toward space complexity — only auxiliary structures. The IBM-preferred answer uses O(1) AUXILIARY space, even though the output itself is O(n). State this distinction explicitly when discussing complexity.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →