Product Except Self

Python data engineering interview problem. Difficulty: beginner. Pattern: Arrays. About 10 minutes. Free to practice.

For each index, product of all other elements without division. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Given a list of numbers, you want a new list where each position holds the product of all the other numbers (every number except the one at that position). Write a function product_except_self(nums) that returns a list output where output[i] is the product of all values in nums except nums[i]. You may not use division. For [1, 2, 3, 4] the answer is [24, 12, 8, 6]. For example, the first value is 2 times 3 times 4, which is 24. Keep the print lines in the editor.

Requirements

  • Return a list with the same length as the input.
  • Each position must hold the product of every other number, leaving out the number at that position.
  • Do not use division.

Constraints

  • Do not use division. Dividing the total product by each number would be the easy idea, but it breaks when a number is 0.
  • The list has at least 2 numbers.
  • Numbers can be negative or zero.

Examples

Input: product_except_self([1, 2, 3, 4]) Output: [24, 12, 8, 6] Left/right products multiply.

Topics: lakebench, python, prefix, product.

More Python interview questions · All interview problems · Learn data engineering

beginner

Product Except Self

Interview-style drill: For each index, product of all other elements without division.

Implement `product_except_self(nums: list) -> list` where output[i] is the product of all nums[j] for j != i. Do not use division. Example: `[1, 2, 3, 4]` → `[24, 12, 8, 6]`. Keep the harness.