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. **Examples:** ```typescript productExceptSelf([1, 2, 3, 4]) // [24, 12, 8, 6] productExceptSelf([-1, 1, 0, -3, 3]) // [0, 0, 9, 0, 0] ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(n)
- Space complexity: O(1)
Two linear passes compute prefix products then suffix products in O(n) total time.
Follow-Up Questions Interviewers Ask About Product of Array Except Self
- Why avoid dividing the total product by nums[i]?
Division fails when nums contains zeros (division by zero), and the problem explicitly forbids the division operator.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.