Skip to main content

93. Product of Array Except Self

mediumAsked at Vercel

Short answer

  • Best approach: Prefix * suffix in O(1) extra space. First pass: out[i] = product of nums[0..i). Second pass: maintain suffix product, multiply into out[i].
  • Time O(n), space O(1) extra
  • How often Vercel asks it: frequently asked · Verified

For each index, return the product of all other elements, without using division and in O(n). Vercel asks this for the prefix-product + suffix-product technique — same shape as their join-cost calculator across multiple parallel deploy graphs.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)— Vercel platform onsite; prefix/suffix product expected.
  • Blind (2026-Q1)— Listed in Vercel routing engineer screen.

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

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 / element

Multiply everything; divide by each element.

Time
O(n)
Space
O(1)
// Disallowed: 'without using division'. Also breaks on zeros.

Tradeoff: Disallowed and broken on zeros.

2. Prefix * suffix in O(1) extra space (optimal)

First pass: out[i] = product of nums[0..i). Second pass: maintain suffix product, multiply into out[i].

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

Tradeoff: Two passes, O(1) extra (output doesn't count). The trick: first pass stores prefix products IN THE OUTPUT array; second pass multiplies in suffix products.

Vercel-specific tips

Vercel grades the O(1) extra space version (output array doesn't count). Bonus signal: noting that you reuse the output array as scratch space for prefix products, then multiply in suffix products on the second pass.

Common mistakes

  • Allocating both prefix and suffix arrays separately — works but O(n) extra space.
  • Computing total / element — disallowed AND breaks on zero (becomes 0/0 = NaN).
  • Forgetting out[0] = 1 — the prefix product of nothing is 1.

Follow-up questions

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

  • Allow division — what's the edge case? Zeros.
  • Range Product Queries — preprocessing for fast queries.
  • Maximum Product Subarray (LC 152).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Ctrl+Enter to execute

FAQ

Why is the output array 'free'?

By LC convention, output space doesn't count as 'extra' space. So an O(n) output + O(1) extra is considered O(1) space.

How do you handle zeros?

The prefix/suffix approach handles zeros naturally — a zero at index i makes every product NOT containing index i equal to zero, and out[i] equals product-of-others which excludes the zero.

Originally published on InterviewChamp.AI

Companies that also ask Product of Array Except Self