Product Array Coding Interview Question |
Interview Question

Product Array

Course launching November 17, 2020

Follow me on YouTube for free coding interview videos.

Users who sign up for the email list will receive an exclusive 75% discount at launch.

Your subscription could not be saved. Please try again.
Your subscription was successful! 🤓

Given an array of integers, write a function buildProductArray that returns an array where each item is the product of all the items in the input array except for the item at that index.


  1. Solve this without using division
  2. You can create a results array, and it won't count against your space complexity
  3. Memory may be a concern though, so try to limit your use of additional data structures
// Input
const input = [1, 2, 3, 4, 5];

// Output: buildProductArray(input)
// [2*3*4*5, 1*3*4*5, 1*2*4*5, 1*2*3*5, 1*2*3*4]
[120, 60, 40, 30, 24];

Validate My Answer

  1. You may immediately recognize a brute force O(n2) answer. You can get this down to O(n).

  2. You may want to add additional arrays to get the O(n) time complexity. However, you can solve this without any additional data structures other than your results array.

  3. To get it to O(n) without additional arrays, you may have to perform sequential O(n) time complexity operations.

  4. Does your solution handle 0 or negative numbers?