Problem
You get an integer array nums. Return an array answer of the same length. The value answer[i] is the product of all the items of nums except nums[i].
A prefix is a part of the array at its start, and a suffix is a part at its end. The product of any prefix or any suffix of nums fits in a 32-bit integer.
Your solution must run in O(n) time and must not use division.
Example 1
- Input:
- nums = [2,3,5]
- Output:
- [15,10,6]
- Explanation:
- For index 0, the product is
3 * 5, which is 15.
Example 2
- Input:
- nums = [4,0,2,-1]
- Output:
- [0,-8,0,0]
- Explanation:
- Each product except
answer[1]includes the 0. So onlyanswer[1]is not 0.
Example 3
- Input:
- nums = [-1,-2,-3,-4]
- Output:
- [-24,-12,-8,-6]
Constraints
- 2 <= nums.length <= 10^5
- -30 <= nums[i] <= 30
- The product of any prefix or suffix of nums fits in a 32-bit integer.
Follow-up
Use only O(1) extra space. The output array does not count as extra space.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Contiguous Array
Coding · Array · Hash Table · Prefix Sum
- Split Array Largest Sum
Coding · Array · Binary Search · Dynamic Programming
- Subarray Product Less Than K
Coding · Array · Binary Search · Prefix Sum
- Count Non Decreasing Arrays With Given Digit Sums
Coding · Array · Dynamic Programming · Prefix Sum
Sources · 3 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Glassdoor · 1 record
Snap
Product of Array Except for Self
Reported Mar 2024 · 1 source report
Software Engineer
Source snapshot: Nov 12, 2026
View source
Hello Interview · 1 record
Meta
Leetcode 238. Product of Array Except Self
Reported Sep 1, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 4 records
Amazon
Product of Array Except Self
Reported date not specified · Source window: 3mo
Source snapshot: Oct 20, 2026
Source frequency score: 62.8. This is not an interview probability.
View source