Back to Practice
#0258

Product of Array Except Self

MediumDSA28 min20 XP

Problem

Given nums, return answer where answer[i] is the product of all values except nums[i]. Do not use division.

Why This Matters

Prefix/suffix accumulation is the array pattern behind many no-division and left-right context problems.

Function Signature

def product_except_self(nums):

Examples

Example 1
Inputnums = [1, 2, 3, 4]
Output[24, 12, 8, 6]

For index 1, multiply everything except 2: 1 * 3 * 4 = 12.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Positive values
Input[1,2,3,4]
Expected[24, 12, 8, 6]

Each output excludes the value at the same index.

Contains zero
Input[-1,1,0,-3,3]
Expected[0, 0, 9, 0, 0]

Only the zero position gets the product of non-zero values.

Hidden Test Categories
two zerosnegative valueslength two input