Skip to interview prep
Apexed

34. Product of Array Except Self

MediumArrays & hashing

Return an array where element i is the product of every element of nums except nums[i]. Do not use division, and aim for O(n) time.

Example 1

Input: nums = [1, 2, 3, 4]
Output: [24, 12, 8, 6]

Constraints

  • 2 ≤ nums.length ≤ 10⁵
  • Products fit in a 32-bit integer

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Product of Array Except Self — coding interview practice — Apexed