238. Product of Array Except Self
mediumAsked at AkamaiCompute for each element the product of all other elements, without using division. Akamai asks this to test prefix/suffix scan thinking — the no-division constraint forces an elegant two-pass pattern that mirrors checksum computation across distributed edge nodes.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Akamai loops.
- Glassdoor (2026-Q1)— Akamai SWE interview reports cite this as a standard prefix-product question in coding rounds.
- Blind (2025-10)— Akamai threads list Product of Array Except Self as a differentiating medium problem testing constraint-aware 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, etc.
Example 2
nums = [-1,1,0,-3,3][0,0,9,0,0]Explanation: Any position without the zero becomes zero; the zero position itself is (-1)*1*(-3)*3=9.
Approaches
1. Prefix and suffix products (two-pass)
First pass: build a prefix product array where prefix[i] = product of all elements to the left of i. Second pass: multiply by a running suffix product (product of all elements to the right of i). This gives each answer[i] = prefix[i] * suffix[i].
- Time
- O(n)
- Space
- O(1) extra (output array not counted)
function productExceptSelf(nums) {
const n = nums.length;
const result = new Array(n);
// First pass: result[i] = product of all elements to the left
result[0] = 1;
for (let i = 1; i < n; i++) {
result[i] = result[i - 1] * nums[i - 1];
}
// Second pass: multiply by running suffix product from the right
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 is excluded per the problem's space analysis convention). The two-pass pattern is the canonical O(1)-space solution Akamai expects.
Akamai-specific tips
Explain why division fails before offering the solution: 'Division breaks on zeros — if nums contains one zero, every answer is 0 except the position of the zero itself, which is the product of all non-zero elements. Two zeros means every position is 0. These cases need special handling, making division-based approaches fragile.' Then introduce the prefix/suffix scan as a clean, uniform solution. Akamai prizes correctness reasoning before code.
Common mistakes
- Attempting a division-based approach and then discovering the zero edge case mid-solution — leads to messy branching.
- Using two separate prefix and suffix arrays — correct but O(n) extra space. The interviewer will ask for the O(1) optimization.
- Initializing result[0] to nums[0] instead of 1 — the left product of the first element is empty (= 1, the multiplicative identity).
Follow-up questions
An interviewer at Akamai may pivot to one of these next:
- What if zeros are possible and you use division — how many distinct zero cases must you handle?
- Maximum Product Subarray (LC 152) — product-based DP with sign changes.
- How would you parallelize this computation across distributed nodes?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the output array not counted toward space complexity?
The problem requires returning n values, so an O(n) output is unavoidable. By convention, required output arrays are excluded from auxiliary space analysis. The question is whether we need additional O(n) data structures beyond the output.
What is the multiplicative identity and why does it matter here?
The multiplicative identity is 1 — multiplying by 1 changes nothing. result[0] starts at 1 because there are no elements to the left of index 0; the empty product is 1 by definition.
Practice these live with InterviewChamp.AI
Drill Product of Array Except Self and other Akamai interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →