Skip to main content

238. Product of Array Except Self

mediumAsked at Broadcom

Compute the product of all elements except the current one without using division. Broadcom asks this because the prefix/suffix scan pattern directly maps to computing cumulative metrics — like packet-count products across an interface chain — without revisiting processed data.

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

Source citations

Public interview reports confirming this problem appears in Broadcom loops.

  • Glassdoor (2026-Q1)Listed in Broadcom SWE interview reports as a prefix-product problem testing O(n) array-scan thinking.
  • Blind (2025-11)Broadcom candidates report this as a medium question that trips up candidates who reach for division.

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) 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 in the array zeroes out all products except those where the zero is the excluded element.

Approaches

1. Left and right prefix arrays

Build a left-products array (product of all elements to the left of i) and a right-products array (product of all elements to the right of i). The answer[i] = left[i] * right[i].

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

Tradeoff: O(n) time, O(n) extra space. Clear and easy to verify. The output array is not counted as extra space per the problem.

2. Space-optimised (O(1) extra space)

Use the output array to store left products first. Then do a single right-to-left pass with a running right-product variable, multiplying into the output array in place.

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

Tradeoff: O(1) extra space beyond the output array. Two passes, no division. This is the answer Broadcom expects for follow-up 'can you reduce space?'

Broadcom-specific tips

At Broadcom, present the two-array approach first to show clarity of thought, then offer the space optimisation: 'I can reuse the output array for left products and accumulate right products in a single variable — bringing extra space to O(1).' Broadcom interviewers appreciate candidates who proactively optimise without prompting. Mention zero-handling: zeros in the array create interesting edge cases worth walking through.

Common mistakes

  • Using division — the problem explicitly forbids it.
  • Not handling zeros in the array — a single zero makes most outputs 0, except where that zero is excluded.
  • Off-by-one: left[0] should be 1 (no elements to the left of index 0).
  • Counting the output array as extra space — the standard interpretation is that it does not count.

Follow-up questions

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

  • What if there are multiple zeros in the array?
  • What if you need to handle very large products (overflow) — use log-sum instead of products.
  • How would you parallelise this prefix-product computation across multiple cores?

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 can't I just use division?

The problem forbids it. Also, division fails when any element is zero. The prefix/suffix approach handles zeros correctly without special-casing.

How many zeros can cause all-zero outputs?

One zero makes all outputs zero except at the index of the zero itself (which equals the product of all other elements). Two or more zeros make every output zero.

Is O(1) extra space truly achievable?

Yes — the output array itself stores intermediate values. The convention is that the required output array doesn't count toward auxiliary space.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →