Back to Practice
#0132

Product Except Self

MediumDSA25 min20 XP

Problem

Given nums, return an array where result[i] is the product of every nums[j] except nums[i]. Do not use division.

Why This Matters

Prefix and suffix passes are a powerful alternative when division is banned or zero values exist.

Function Signature

def product_except_self(nums):

Examples

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

For index 1, multiply 1 * 3 * 4 = 12.

Constraints

  • Return the exact requested structure.
  • Handle normal edge cases cleanly.
  • Prefer readable code over clever shortcuts.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input[1, 2, 3, 4]
Expected[24, 12, 8, 6]

For index 1, multiply 1 * 3 * 4 = 12.

Hidden Test Categories
one zerotwo zerosnegative values