← Questions
Product of array except self
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's HackerRank assessment and at Amazon and Meta, last asked Feb 2026. It tests arrays and prefix sums.
Given an integer array, return an array where position i holds the product of every element except nums[i]. Do it without division, in linear time.
Interface
Unless you agree on a different interface with the interviewer, assume:
product_except_self(nums: list[int]) -> list[int]
Example
product_except_self([1, 2, 3, 4]) -> [24, 12, 8, 6]
product_except_self([-1, 1, 0, -3, 3]) -> [0, 0, 9, 0, 0] # only the zero's own slot is non-zero
product_except_self([0, 0, 2]) -> [0, 0, 0]
Constraints
-
2 <= len(nums) <= 10^5,-30 <= nums[i] <= 30. - Every prefix and suffix product fits in a 32-bit integer.
- No division. O(n) time.
Clarifying questions
- Why no division: because of zeros, or as a rule?
- Can the products overflow?
- Does the output array count as extra space?
What a strong answer covers
-
answer[i]= product of everything left ofitimes product of everything right ofi. - First with two helper arrays of prefix and suffix products, O(n) time and O(n) space.
- Then O(1) extra space besides the output: store prefix products in the output, then multiply in a running suffix product from the right.
- Why division fails with zeros, and how many zeros change the answer (none, one, two or more).
- Tests: one zero, two zeros, negative numbers, length 2.
Follow-ups
- The values change between queries. Answer "product of all except
i" quickly after updates (a segment tree). - Products can overflow 64 bits. Return them modulo a prime, where division by an inverse is not always possible.
- Return the product of every element except a range
[l, r].
Where it's asked
- Microsoft · HackerRank assessment · Last asked Feb 202690 min · Two problems in a locked full-screen browser
- Amazon · Coding · Last asked Jan 202655 min · Usually two rounds; one is often an object-oriented design built in steps · No AI
- Meta · CodeSignal assessment · Last asked Aug 202590 min · Four levels on one class: basic operations, a ranking query, timestamps and expiry, then merge or snapshot and restore · No AI
What it tests
arrays · prefix sums
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding


