Skip to main content

238. Product of Array Except Self

mediumAsked at Juniper Networks

Return an array where each element is the product of all other elements, without using division. Juniper tests this because the left-right prefix scan pattern appears in networking contexts like computing per-interface cumulative traffic without a global total — the trick of two passes is a systems-thinking insight.

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

Source citations

Public interview reports confirming this problem appears in Juniper Networks loops.

  • Glassdoor (2025-Q4)Mentioned in Juniper SWE onsite reports as a creative prefix-scan problem.
  • Blind (2025-10)Listed in Juniper coding prep threads as a must-know medium array problem.

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, etc.

Example 2

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

Explanation: The zero dominates most products.

Approaches

1. Two-pass prefix and suffix products

Build a left-products array (product of all elements to the left of i) and a right-products array (product of all to the right). Multiply them together.

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 and O(n) space. Clear and correct. The interviewer will then ask for the O(1) extra space version.

2. Single output array + running right product

Use the output array itself as the left-products array. Then do a right-to-left pass maintaining a running right-product variable, multiplying it into the output in-place.

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

Tradeoff: O(n) time, O(1) extra space (the output array does not count as extra). This is the optimal solution Juniper expects.

Juniper Networks-specific tips

The constraint 'no division' is deliberate — it rules out the obvious shortcut of computing the total product and dividing. Mention this explicitly to show you read the constraints. Present the two-array solution first, then upgrade to the O(1)-space version. Juniper control-plane engineers appreciate the in-place running-product trick — it mirrors techniques used in packet processing pipelines where you carry state forward without allocating per-packet buffers.

Common mistakes

  • Using division — explicitly forbidden by the constraint.
  • Not initializing result[0] = 1 for the left-products pass — there is nothing to the left of index 0.
  • Multiplying nums[i] into rightProduct before updating result[i] — the running product must be applied before being updated.
  • Forgetting to handle zeros — zeros in the input are handled correctly by the multiplicative approach without special casing.

Follow-up questions

An interviewer at Juniper Networks may pivot to one of these next:

  • What if there were zeros in the array and you had to use division?
  • Maximum Product Subarray (LC 152) — related product manipulation over subarrays.
  • How would you compute running prefix products in a streaming fashion where elements arrive one at a time?

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 output array not count toward space complexity?

By convention in algorithm analysis, the output that is required by the problem specification does not count as extra auxiliary space — we would need it regardless.

How are zeros handled?

Correctly and automatically. If nums[i] = 0, all result values except result[i] will include 0 in their product, making them 0. result[i] itself equals the product of all non-zero elements — all computed correctly by the prefix-suffix multiplication.

What is the right-pass update order?

First multiply result[i] by rightProduct (the suffix product from i+1 onward), then multiply rightProduct by nums[i] to include position i in future suffix products.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →