238. Product of Array Except Self
mediumAsked at DRWDRW uses this problem to test prefix-product reasoning without division — the same technique used to compute portfolio-weight normalization factors and running exposure metrics without re-scanning the full array. The O(n) no-division constraint is the key DRW signal.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in DRW loops.
- Glassdoor (2026-Q1)— DRW SWE candidates report prefix-product and running-aggregate problems as medium-difficulty staples in onsite coding rounds.
- Blind (2025-10)— DRW threads cite Product of Array Except Self as a problem where the no-division constraint is taken seriously — candidates who use total product / nums[i] are corrected.
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: Zero in the array makes most products zero.
Approaches
1. Prefix and suffix products (two arrays)
Build a prefix-product array and a suffix-product array. answer[i] = prefix[i-1] × suffix[i+1].
- Time
- O(n)
- Space
- O(n) extra
function productExceptSelf(nums) {
const n = nums.length;
const prefix = new Array(n).fill(1);
const suffix = new Array(n).fill(1);
for (let i = 1; i < n; i++) prefix[i] = prefix[i-1] * nums[i-1];
for (let i = n-2; i >= 0; i--) suffix[i] = suffix[i+1] * nums[i+1];
return nums.map((_, i) => prefix[i] * suffix[i]);
}Tradeoff: O(n) time, O(n) extra space for the two auxiliary arrays. Correct and easy to understand. Mention the O(1) space optimization immediately.
2. O(1) space (output array + running suffix)
Use the output array itself as the prefix product. Then make a second right-to-left pass, accumulating the suffix product in a single variable and multiplying into the output.
- Time
- O(n)
- Space
- O(1) extra (output excluded)
function productExceptSelf(nums) {
const n = nums.length;
const answer = new Array(n).fill(1);
// First pass: answer[i] = product of nums[0..i-1]
for (let i = 1; i < n; i++) answer[i] = answer[i-1] * nums[i-1];
// Second pass: multiply in suffix product from right
let suffix = 1;
for (let i = n-1; i >= 0; i--) {
answer[i] *= suffix;
suffix *= nums[i];
}
return answer;
}Tradeoff: O(n) time, O(1) extra space. This is the canonical DRW answer — lead with the two-array version to show clarity, then immediately optimize to O(1).
DRW-specific tips
DRW expects the O(1) space version without prompting. After coding, they will ask: 'What happens when nums contains one zero? Two zeros?' Walk through: one zero → all answers are 0 except the position of the zero itself. Two zeros → all answers are 0. The prefix-suffix algorithm handles this correctly without special casing. Also: 'How would you compute a rolling product-except-self over a sliding window of size k?' — that requires a deque-based approach similar to Sliding Window Maximum.
Common mistakes
- Using division (total product / nums[i]) without flagging zero-division risk — explicitly banned by the constraint.
- Allocating two O(n) arrays and stopping there — always offer the O(1) optimization.
- Off-by-one in prefix initialization — prefix[0] = 1 (product of zero elements), not nums[0].
- Not considering negative numbers — the algorithm is multiplication-based and handles negatives correctly.
Follow-up questions
An interviewer at DRW may pivot to one of these next:
- How does the algorithm change if nums may contain zeros — what is the maximum profit-loss implication of a zero-filled portfolio weight vector?
- Trapping Rain Water (LC 42) — uses the same prefix/suffix pattern to compute left-max and right-max arrays.
- How would you compute a product-except-self over a live sliding window of size k?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is division banned?
Division fails when any nums[i] = 0 (division by zero). The prefix-suffix approach is defined for all inputs including zeros.
How does the O(1) space trick work?
The output array stores prefix products after the first pass. The second pass accumulates the suffix product in a scalar and multiplies it into each position — no extra array needed.
How does this appear at DRW?
Computing per-instrument contribution to portfolio P&L requires multiplying all other instruments' weights — structurally identical to product-except-self. The O(1) space property matters when the instrument universe is large.
Practice these live with InterviewChamp.AI
Drill Product of Array Except Self and other DRW interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →