Problem · Array

Product of Array Except Self

Learn this problem
MediumNew RelicONSITE INTERVIEW

Problem statement

Given an integer array nums, return an array answer where answer[i] is the product of every element of nums except nums[i].

Do not use division.

Interview Follow-up

The interviewer asked for the most optimized solution from the start and then had the candidate run two test cases.

Function

productExceptSelf(nums: int[]) → int[]

Examples

Example 1

nums = [1,2,3,4]return = [24,12,8,6]

Each position contains the product of all values except the one at that position.

Example 2

nums = [-1,1,0,-3,3]return = [0,0,9,0,0]

Only the index containing 0 receives the product of all nonzero values.

drafts saved locally
public int[] productExceptSelf(int[] nums) {
  // write your code here
}
nums[1,2,3,4]
expected[24,12,8,6]
checking account