← Core Algorithms
#8 most askedVery commonMediumPrefix sum

Product of Array Except Self

New to Prefix sum? Learn it from scratch first
Prefix Sums β†’
🏒 Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

Return a list where answer[i] is the product of every number in nums except nums[i]. Do it in O(n) without division.

[1, 2, 3, 4] β†’ [24, 12, 8, 6]

πŸ§ͺ Practice

Write your solution, then press Run tests. Explain your approach out loud as you go.

Python
def product_except_self(nums):
    pass

Output appears here. Press Run or Ctrl/⌘ + Enter.

Try it first. Struggling for a few minutes is how it sticks.