79. Product of Array Except Self
mediumAsked at WorkdayReturn an array where output[i] is the product of all elements except nums[i], without division and in O(n). Workday uses this for prefix/suffix product fluency — same shape as computing aggregate-without-self metrics across departments.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Workday loops.
- Glassdoor (2025-Q4)— Workday SDE2 onsite — array manipulation staple.
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] <= 30Follow-up: Can you solve it in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
Examples
Example 1
nums = [1,2,3,4][24,12,8,6]Example 2
nums = [-1,1,0,-3,3][0,0,9,0,0]Approaches
1. Division (forbidden)
Compute total product; divide by each element.
- Time
- O(n)
- Space
- O(1)
// disallowed; also breaks on zeroTradeoff: Forbidden by prompt. Also fails on inputs containing 0.
2. Two-pass prefix/suffix
Pass 1 left-to-right: output[i] = product of all before i. Pass 2 right-to-left: multiply output[i] by product of all after i.
- Time
- O(n)
- Space
- O(1) extra)
function productExceptSelf(nums) {
const output = new Array(nums.length);
let prefix = 1;
for (let i = 0; i < nums.length; i++) {
output[i] = prefix;
prefix *= nums[i];
}
let suffix = 1;
for (let i = nums.length - 1; i >= 0; i--) {
output[i] *= suffix;
suffix *= nums[i];
}
return output;
}Tradeoff: O(1) extra space (output doesn't count). The prefix-then-suffix accumulation avoids both division and the all-zero failure mode.
Workday-specific tips
Workday wants the two-pass approach. The output array as scratch space is the trick to meet O(1) extra space. State this when explaining your plan.
Common mistakes
- Using division — forbidden, plus crashes on zero.
- Allocating prefix and suffix arrays — O(n) extra space, fails the follow-up.
- Initializing prefix or suffix to 0 — should be 1 (multiplicative identity).
Follow-up questions
An interviewer at Workday may pivot to one of these next:
- Allow division and handle zeros gracefully.
- Maximum Product Subarray (LC 152).
- What if the array is a stream?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why O(1) extra space?
We mutate the output array in two passes. Prefix and suffix are scalar accumulators, not arrays. Output doesn't count per the prompt.
Zero handling?
Naturally handled by the two-pass. If nums contains one zero, only that index is non-zero in the output. If two or more zeros, the output is all zeros.
Practice these live with InterviewChamp.AI
Drill Product of Array Except Self and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →