238. Product of Array Except Self
mediumAsked at AppleProduct of Array Except Self is Apple's beloved 'no division' question. Two passes — left-products then right-products into the same output array — gives O(n) time with O(1) extra space. Apple grades on whether you avoid division entirely and handle the multi-zero case.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Apple loops.
- Glassdoor (2026-Q1)— Apple SWE phone-screen reports list this as a recurring 30-minute array medium.
- Blind (2025-12)— Apple ICT3/ICT4 reports cite Product of Array Except Self as the canonical no-division array 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] <= 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]Example 2
nums = [-1,1,0,-3,3][0,0,9,0,0]Approaches
1. Two passes into output array (optimal O(1) extra space)
First pass: result[i] = product of all elements LEFT of i. Second pass: walk right-to-left, multiplying by running product of elements RIGHT of i.
- Time
- O(n)
- Space
- O(1) excluding output
function productExceptSelf(nums) {
const n = nums.length;
const result = new Array(n);
result[0] = 1;
for (let i = 1; i < n; i++) result[i] = result[i - 1] * nums[i - 1];
let right = 1;
for (let i = n - 1; i >= 0; i--) {
result[i] = result[i] * right;
right *= nums[i];
}
return result;
}Tradeoff: Two linear passes, no division, no extra arrays beyond the required output. The 'output array doesn't count' is a common LeetCode convention — be ready to confirm with the interviewer.
2. Left and right product arrays
Precompute leftProduct[i] = product of all elements to the left, rightProduct[i] = product to the right. result[i] = leftProduct[i] * rightProduct[i].
- Time
- O(n)
- Space
- O(n) extra
function productExceptSelf(nums) {
const n = nums.length;
const left = new Array(n).fill(1);
const right = new Array(n).fill(1);
for (let i = 1; i < n; i++) left[i] = left[i - 1] * nums[i - 1];
for (let i = n - 2; i >= 0; i--) right[i] = right[i + 1] * nums[i + 1];
return left.map((v, i) => v * right[i]);
}Tradeoff: Cleaner to reason about (two separate arrays for left and right) but extra space. The optimal version inlines the right traversal into a single int.
3. Division (rejected per spec)
Compute total product, then divide by each element.
- Time
- O(n)
- Space
- O(n)
// Forbidden by the problem: 'without using the division operation.'
// Also breaks on zeros.Tradeoff: Explicitly forbidden. Apple includes this as a reject path — say 'I could divide, but the problem forbids it (and division fails on zeros anyway).'
Apple-specific tips
Apple wants the answer 'product[i] = left-product * right-product, and I can compute both in two passes without extra arrays.' Then they want the zero edge cases discussed: one zero → only the zero's position is nonzero; two or more zeros → result is all zeros. Mention these BEFORE writing code; they're the give-away that you've thought about correctness.
Common mistakes
- Using division on a problem that explicitly forbids it.
- Allocating left[] and right[] arrays when the prompt says O(1) extra space.
- Wrong initialization: result[0] should be 1 (no elements to the left), not nums[0].
Follow-up questions
An interviewer at Apple may pivot to one of these next:
- Product of Array Except Self with overflow — use BigInt.
- If division WERE allowed, how would you handle zeros? (Count zeros: 0 → divide normally; 1 → only that index nonzero; 2+ → all zeros.)
- Maximum Product Subarray (LC 152) — related but different problem.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Does the output array count toward space complexity?
By LeetCode convention no. The 'O(1) extra space' refers to memory beyond the required output. Confirm with the interviewer if unsure.
Why not just use division?
The problem explicitly forbids it (and division would fail on zeros without special handling). The two-pass left/right product trick is what Apple is testing for.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Product of Array Except Self and other Apple interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →