238. Product of Array Except Self
mediumAsked at CohereCompute the product of all elements except each element itself, without division. Cohere asks this because the prefix/suffix product pattern is the same technique used in attention-mask generation and cumulative probability normalisation in beam search.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Cohere loops.
- Glassdoor (2025-Q4)— Cohere engineers cite this as a favourite problem for testing prefix/suffix thinking in SWE interviews.
- Blind (2025-10)— Cohere candidates list Product of Array Except Self among the top medium questions asked in onsites.
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 fits 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: Presence of zero makes most products zero.
Approaches
1. Prefix and suffix products
Build a prefix-product array (product of all elements to the left) and a suffix-product array (product of all elements to the right). The answer for each index is prefix[i] * suffix[i].
- Time
- O(n)
- Space
- O(n)
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 prefix.map((p, i) => p * suffix[i]);
}Tradeoff: Clear and correct. Uses O(n) extra space for two auxiliary arrays.
2. O(1) extra space (output array as prefix, running suffix)
Use the output array itself to store prefix products. Then make a second right-to-left pass with a running suffix product, multiplying into the output array.
- Time
- O(n)
- Space
- O(1) extra (output array not counted)
function productExceptSelf(nums) {
const n = nums.length;
const result = new Array(n).fill(1);
// Left pass: result[i] = product of nums[0..i-1]
for (let i = 1; i < n; i++) result[i] = result[i - 1] * nums[i - 1];
// Right pass: multiply running suffix into result
let suffix = 1;
for (let i = n - 1; i >= 0; i--) {
result[i] *= suffix;
suffix *= nums[i];
}
return result;
}Tradeoff: O(1) extra space beyond the required output array — the optimal solution Cohere expects once they ask you to eliminate the suffix array.
Cohere-specific tips
Lead with the two-array approach to demonstrate understanding of the prefix/suffix product decomposition, then proactively optimise to O(1) extra space: 'I can reuse the output array for the prefix pass and fold the suffix into a running variable on the way back.' Cohere engineers working on inference pipelines care about memory efficiency — this optimisation signals that mindset.
Common mistakes
- Using division — explicitly banned by the problem statement.
- Off-by-one in prefix initialisation — prefix[0] should be 1 (no elements to the left of index 0).
- Forgetting to initialise suffix to 1 before the right-to-left pass.
- Handling zeros incorrectly — the prefix/suffix approach handles zeros naturally, unlike the division approach.
Follow-up questions
An interviewer at Cohere may pivot to one of these next:
- What if there are multiple zeros in the array?
- How would you compute this for a matrix where each row's product excludes the diagonal element?
- Trapping Rain Water — another problem with left-pass/right-pass structure.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is division not allowed?
Division by zero is undefined when nums[i] = 0. Handling all zero cases manually negates the simplicity. The prefix/suffix approach avoids division entirely.
Does the space-optimised solution still allocate O(n)?
The output array is O(n) but is considered part of the required output, not auxiliary space. The only extra variable is the running suffix — hence O(1) extra.
How does this relate to inclusive prefix products?
The prefix array here is exclusive (does not include nums[i] itself). Adding nums[i] at the end of each prefix would give an inclusive prefix product, which is standard in range-query problems.
Practice these live with InterviewChamp.AI
Drill Product of Array Except Self and other Cohere interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →