But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.
You're in!
You have an array of integers, and for each index you want to find the product of every integer except the integer at that index.
Write a function getProductsOfAllIntsExceptAtIndex() that takes an array of integers and returns an array of the products.
For example, given:
[1, 7, 3, 4]
your function would return:
[84, 12, 28, 21]
by calculating:
[7 * 3 * 4, 1 * 3 * 4, 1 * 7 * 4, 1 * 7 * 3]
Here's the catch: You can't use division in your solution!
To find the products of all the integers except the integer at each index, we'll go through our array greedily twice. First we get the products of all the integers before each index, and then we go backwards to get the products of all the integers after each index.
When we multiply all the products before and after each index, we get our answer—the products of all the integers except the integer at each index!
void getProductsOfAllIntsExceptAtIndex(
const int *intArray,
size_t intArrayLength,
int *productsOfAllIntsExceptAtIndexOutput,
size_t productsOfAllIntsExceptAtIndexOutputLength)
{
int productSoFar;
size_t i;
// getting the product of numbers at other indices requires at least 2 numbers
assert(intArrayLength >= 2);
// make sure output is large enough
assert(productsOfAllIntsExceptAtIndexOutputLength >= intArrayLength);
// for each integer, we find the product of all the integers
// before it, storing the total product so far each time
productSoFar = 1;
for (i = 0; i < intArrayLength; i++) {
productsOfAllIntsExceptAtIndexOutput[i] = productSoFar;
productSoFar *= intArray[i];
}
// for each integer, we find the product of all the integers
// after it. since each index in products already has the
// product of all the integers before it, now we're storing
// the total product of all other integers
productSoFar = 1;
for (i = intArrayLength; i > 0; i--) {
productsOfAllIntsExceptAtIndexOutput[i - 1] *= productSoFar;
productSoFar *= intArray[i - 1];
}
}
time and space. We make two passes through our input an array, and the array we build always has the same length as the input array.
What if you could use division? Careful—watch out for zeroes!
Another question using a greedy approach. The tricky thing about this one: we couldn't actually solve it in one pass. But we could solve it in two passes!
This approach probably wouldn't have been obvious if we had started off trying to use a greedy approach.
Instead, we started off by coming up with a slow (but correct) brute force solution and trying to improve from there. We looked at what our solution actually calculated, step by step, and found some repeat work. Our final answer came from brainstorming ways to avoid doing that repeat work.
So that's a pattern that can be applied to other problems:
Start with a brute force solution, look for repeat work in that solution, and modify it to only do that work once.
Reset editor
Powered by qualified.io