Blind 75
6 / 75Free practice

Product of Array Except Self

Medium · Arrays & hashing

Jump to code

For each position, return the product of every array element except the one at that position. Use no division and aim for linear time. The array has at least two integers.

Examples

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 <= 1000
  • -20 <= nums[i] <= 20
  • Each output product fits a signed 32-bit integer.
Need a hint?

Separate the elements before an index from the elements after it.

Asked in real interviews

Candidates reported versions of Product of Array Except Self from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 10 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    1,
    2,
    3,
    4
  ]
]
Expected
[
  24,
  12,
  8,
  6
]
Case 2
Input
[
  [
    -1,
    1,
    0,
    -3,
    3
  ]
]
Expected
[
  0,
  0,
  9,
  0,
  0
]
Case 3
Input
[
  [
    2,
    3,
    4,
    5
  ]
]
Expected
[
  60,
  40,
  30,
  24
]
Case 4
Input
[
  [
    1,
    0
  ]
]
Expected
[
  0,
  1
]
Case 5
Input
[
  [
    0,
    0,
    2
  ]
]
Expected
[
  0,
  0,
  0
]

Drafts are saved per language in this browser when storage is available.