Skip to main content

75. Product of Array Except Self

mediumAsked at Salesforce

Return an array where each element is the product of all other elements (no division allowed). Salesforce uses this to test prefix-products and the no-division trick.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses cumulative products in their commission calculation.
  • Blind (2025-09)Common Salesforce backend phone-screen question.

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 (forbidden)

Compute total product; divide by each element.

Time
O(n)
Space
O(1)
// Forbidden: division. Also fails on zeros.

Tradeoff: Explicitly forbidden. Also breaks on zeros.

2. Prefix products + suffix products

result[i] = prefix product (elements before i) * suffix product (elements after i). Use the result array itself for the prefix; sweep right-to-left for the suffix.

Time
O(n)
Space
O(1) excluding output
function productExceptSelf(nums) {
  const n = nums.length;
  const result = new Array(n);
  let prefix = 1;
  for (let i = 0; i < n; i++) {
    result[i] = prefix;
    prefix *= nums[i];
  }
  let suffix = 1;
  for (let i = n - 1; i >= 0; i--) {
    result[i] *= suffix;
    suffix *= nums[i];
  }
  return result;
}

Tradeoff: Two passes; O(1) extra space (the result array isn't counted). The trick: stash prefix products in result, then multiply by suffix products in reverse.

Salesforce-specific tips

Salesforce specifically tests the 'use the output array as scratch space' trick. They grade on whether you can derive the two-pass O(1)-extra-space solution. Bonus signal: handle the zeros gracefully — the algorithm works without special-casing them, unlike the division approach.

Common mistakes

  • Using two extra arrays for prefix and suffix — O(n) extra space when O(1) is achievable.
  • Combining prefix and suffix in one pass — requires extra storage for the running totals.
  • Trying to use division and then handling zeros as a special case — Salesforce will fail this immediately.

Follow-up questions

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

  • Trapping Rain Water (LC 42) — similar prefix/suffix pattern.
  • Product of Array Except Self II (with zeros allowed) — already handled here.
  • Maximum Product Subarray (LC 152).

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 no division allowed?

Because division by zero is ambiguous. Forcing the no-division solution makes the algorithm robust to zeros.

Is the output array considered extra space?

By convention, no. The problem requires O(1) extra space, where the output is exempt. The two-pass approach uses two scalars (prefix and suffix) — that's O(1).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →