238. Product of Array Except Self
mediumAsked at HubSpotHubSpot includes this problem to test prefix/suffix product reasoning without division — a constraint that forces you to think about the problem from two directions simultaneously, a hallmark of the kind of algorithmic clarity their Boston engineering team values.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in HubSpot loops.
- Glassdoor (2026-Q1)— HubSpot candidates report Product of Array Except Self as a recurring onsite problem emphasizing the no-division constraint.
- Blind (2025-09)— HubSpot SWE threads list this as a high-signal medium problem for testing prefix-product thinking.
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] <= 30The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
Examples
Example 1
nums = [1,2,3,4][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
nums = [-1,1,0,-3,3][0,0,9,0,0]Explanation: The zero makes all positions with a zero factor equal to 0.
Approaches
1. Two-pass prefix/suffix products
First pass: build prefix products (product of all elements to the left of i). Second pass: multiply each prefix product by the suffix product (product of all elements to the right of i), accumulated in a running variable.
- Time
- O(n)
- Space
- O(1) excluding output array
function productExceptSelf(nums) {
const n = nums.length;
const result = new Array(n).fill(1);
// Pass 1: prefix products
let prefix = 1;
for (let i = 0; i < n; i++) {
result[i] = prefix;
prefix *= nums[i];
}
// Pass 2: multiply by suffix products
let suffix = 1;
for (let i = n - 1; i >= 0; i--) {
result[i] *= suffix;
suffix *= nums[i];
}
return result;
}Tradeoff: O(n) time, O(1) extra space (the output array doesn't count). The key insight: result[i] = (product of all elements left of i) × (product of all elements right of i). Two running multipliers replace two separate O(n) arrays.
HubSpot-specific tips
State the constraint before the solution: 'No division, so I can't compute total product and divide. Instead, I'll use prefix products from the left and suffix products from the right.' HubSpot interviewers specifically test whether you mention the zero-handling implication — with division you'd need to special-case zeros; with prefix/suffix products, zeros are handled automatically. Relate it to their analytics context: computing 'all-but-one' aggregates without recomputing the full sum.
Common mistakes
- Using division (total product / nums[i]) — explicitly forbidden and fails on zeros.
- Allocating two separate prefix and suffix arrays — correct but O(n) extra space; the single-array two-pass approach is more elegant.
- Not initializing prefix and suffix running multipliers to 1 — the first/last element should have a prefix/suffix product of 1 (empty product).
- Off-by-one: prefix[i] should be the product of elements BEFORE i, not including i.
Follow-up questions
An interviewer at HubSpot may pivot to one of these next:
- What if division were allowed — how would you handle zeros efficiently?
- Maximum Product Subarray (LC 152) — tracks both max and min running products.
- How would you generalize to a 2D matrix where each cell needs the product of all other cells?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why doesn't the output array count toward space complexity?
By convention, the output array is considered part of the required output and not counted as auxiliary space. The O(1) claim refers to any extra workspace beyond the output array.
How does this handle arrays with zeros?
Automatically and correctly — a zero in the array makes its neighbors' suffix/prefix products zero. The product for the zero's own position is nonzero (the product of everything else). No special-casing needed.
Can you solve this in one pass?
Not in general — you need information from both sides of each element, which requires at minimum seeing the array from both directions. Two passes is optimal.
Practice these live with InterviewChamp.AI
Drill Product of Array Except Self and other HubSpot interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →