As reported: Target Engineer L4 onsite (2025, in person, two-interviewer panels), round 1: Product of Array Except Self — from brute force to optimal, with edge cases, where the code would fail, and negative test cases.
This is LeetCode 238 — Product of Array Except Self. Our write-up: /product-of-array-except-self.
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
Example 1
nums = [1,2,3,4]
Output: [24,12,8,6]
Example 2
nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Constraints: 2 ≤ nums.length ≤ 10^5; -30 ≤ nums[i] ≤ 30; the product of any prefix or suffix fits in a 32-bit integer.
Code runner format: one JSON line: nums. Implement productExceptSelf(nums).