Skip to main content

238. Product of Array Except Self

mediumAsked at HP

HP's numerical calibration and imaging-pipeline software computes normalization factors that exclude each sensor's own reading. Product of Array Except Self — without using division — is a direct test of prefix/suffix product reasoning that appears naturally in HP's signal-processing and correction workflows.

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

Source citations

Public interview reports confirming this problem appears in HP loops.

  • Glassdoor (2026-Q1)HP SWE candidates report this problem in second-round onsite interviews focused on array manipulation.
  • Blind (2025-10)HP interview threads list Product of Array Except Self as a frequently tested medium problem in technical screens.

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 is present — any product that includes the zero position equals zero.

Approaches

1. Prefix and suffix products (two-pass)

First pass: compute left[i] = product of all elements to the left of i. Second pass: multiply by right[i] = product of all elements to the right of 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 nums.map((_, i) => left[i] * right[i]);
}

Tradeoff: O(n) time, O(n) extra space for the two arrays. Clear and easy to explain.

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

Use the output array for left products (first pass), then multiply in the suffix product using a running variable (second pass) — no extra arrays.

Time
O(n)
Space
O(1) extra (output array doesn't count)
function productExceptSelf(nums) {
  const n = nums.length;
  const answer = new Array(n).fill(1);
  // First pass: answer[i] = product of all elements to the left
  let leftProduct = 1;
  for (let i = 0; i < n; i++) {
    answer[i] = leftProduct;
    leftProduct *= nums[i];
  }
  // Second pass: multiply by product of all elements to the right
  let rightProduct = 1;
  for (let i = n - 1; i >= 0; i--) {
    answer[i] *= rightProduct;
    rightProduct *= nums[i];
  }
  return answer;
}

Tradeoff: O(1) extra space (the output array is excluded per the problem convention). HP interviewers reward this optimization.

HP-specific tips

HP interviewers often ask the O(1) space follow-up immediately — anticipate it and present the two-pass approach as an intermediate step before the space-optimized version. Explicitly state that the output array is not counted toward extra space (as per the problem statement), which justifies calling it O(1). If zeroes are in the array, trace through the [-1,1,0,-3,3] example to show the algorithm handles them naturally without special-casing.

Common mistakes

  • Using division — explicitly forbidden by the problem and fails on zero inputs.
  • Recomputing the full product for each element — O(n²) brute force.
  • Initializing left product to nums[0] instead of 1 — off-by-one; left[0] should be 1 (no elements to the left).
  • Forgetting to update the running product after using it — the order matters: assign, then multiply.

Follow-up questions

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

  • What if the input contains multiple zeros — how many zeros can appear and what does the output look like?
  • Extend to a 2D matrix where each cell should contain the product of all other cells.
  • How would you handle floating-point overflow for very large arrays?

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 doesn't the algorithm need special handling for zeros?

If a zero exists at position i, all prefix/suffix products passing through it become zero. The output for position i (which excludes nums[i]) collects the product of all other elements — if they are non-zero, the result is non-zero. The math handles zeros automatically.

Why is the output array excluded from the O(1) space claim?

The output is a required part of the return value, not auxiliary storage. By convention, required output arrays are excluded from space complexity.

Does the order of the two passes matter?

Yes — the left pass must come before the right pass because the right pass multiplies into the array built by the left pass.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →