Skip to main content

238. Product of Array Except Self

mediumAsked at eBay

eBay's pricing engine computes the impact of removing one item from a basket — 'what is the product of all other prices?' This problem tests whether you can achieve O(n) without division by building prefix and suffix products. The no-division constraint is the key challenge and a signal of strong algorithmic thinking.

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

Source citations

Public interview reports confirming this problem appears in eBay loops.

  • Glassdoor (2026-Q1)Listed as a common eBay medium problem emphasizing the no-division constraint.
  • Blind (2025-10)eBay SWE threads consistently mention Product of Array Except Self as a mid-level interview staple.

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: Zero in the array makes most products zero.

Approaches

1. Prefix and suffix product arrays

Build a prefix product array (left[i] = product of all elements before i) and a suffix product array (right[i] = product of all elements after i). 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 for left and right arrays. Clear and easy to explain. Good starting point before optimizing space.

2. O(1) extra space (output array + running suffix)

Use the output array as the prefix product. Make a second pass maintaining a running suffix product, multiplying into the output array in place.

Time
O(n)
Space
O(1) extra (excluding output array)
function productExceptSelf(nums) {
  const n = nums.length;
  const answer = new Array(n).fill(1);
  // First pass: answer[i] holds prefix product (product of all before i)
  for (let i = 1; i < n; i++) answer[i] = answer[i - 1] * nums[i - 1];
  // Second pass: multiply by suffix product on the fly
  let suffix = 1;
  for (let i = n - 1; i >= 0; i--) {
    answer[i] *= suffix;
    suffix *= nums[i];
  }
  return answer;
}

Tradeoff: O(1) extra space — the output array is not counted per the problem statement. This is what eBay expects when they ask 'can you do it in constant extra space?'

eBay-specific tips

eBay interviewers almost always ask the follow-up: 'Can you do it in O(1) extra space?' Have the two-pass solution ready. The insight: use the answer array itself to store prefix products in pass 1, then multiply by suffix in pass 2. Explain the invariant: 'After pass 1, answer[i] = product of everything to the left of i. After pass 2, we multiply by the product of everything to the right.' Also think about zeroes: if there are two or more zeroes, all answers are 0. If exactly one zero, only the position of the zero is non-zero.

Common mistakes

  • Using division (total product / nums[i]) — the problem explicitly prohibits it, and it breaks for arrays containing zero.
  • Off-by-one in prefix/suffix boundaries — left[i] is product of elements before i (0..i-1), not including i.
  • Forgetting to initialize the running suffix to 1 in the O(1) approach.
  • Not considering zeroes — two zeroes make all outputs 0; one zero makes all outputs 0 except at the zero's position.

Follow-up questions

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

  • What if the array could contain zeroes? Handle the one-zero and two-zero cases explicitly.
  • Maximum Product Subarray (LC 152) — related product-based DP problem.
  • How would you extend this to a 2D matrix — product of all elements in the matrix except those in the same row or column?

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 the output array not counted in the O(1) space constraint?

The problem requires returning an output array of size n, so that storage is considered obligatory. 'Extra space' refers to any additional storage beyond the required output.

Does division work if there are no zeroes?

Yes — compute the total product, then divide by each element. But you need special cases for one zero and two+ zeroes. The prefix/suffix approach handles all cases uniformly.

What is the prefix/suffix invariant?

left[i] = nums[0] * nums[1] * ... * nums[i-1] (product of all elements strictly before index i). right[i] = nums[i+1] * ... * nums[n-1] (product of all elements strictly after index i). answer[i] = left[i] * right[i].

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →