Course launching September 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.
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.
- Solve this without using division
- You can create a
resultsarray, and it won't count against your space complexity
- 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
You may immediately recognize a brute force O(n2) answer. You can get this down to O(n).
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
To get it to O(n) without additional arrays, you may have to perform sequential O(n) time complexity operations.
Does your solution handle 0 or negative numbers?