I was asked the following interview question over the phone:
Given an array of integers, produce an array whose values are the product of every other integer
excluding the current index.
Example:
[4, 3, 2, 8] -> [3*2*8, 4*2*8, 4*3*8, 4*3*2] -> [48, 64, 96, 24]
and I gave the following answer
import java.math.BigInteger;
import java.util.Arrays;
public class ProductOfAnArray {
public static void main(String[] args) {
try {
System.out.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4, 3, 2, 8 })));
System.out.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4, 0, 2, 8 })));
System.out.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4, 0, 2, 0 })));
System.out.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] {})));
System.out
.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4, 3, 2, 8, 3, 2, 4, 6, 7,
3, 2, 4 })));
System.out
.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4, 3, 2, 8, 3, 2, 4, 6, 7,
3, 2, 4 })));
System.out.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4432432, 23423423, 34234, 23423428,
4324243, 24232, 2342344, 64234234, 4324247,
4234233, 234422, 234244 })));
} catch (Exception e) {
// debug exception here and log.
}
}
/*
* Problem: Given an array of integers, produce an array whose values are
* the product of every other integer excluding the current index.
*
* Assumptions. Input array cannot be modified. input is an integer array
* "produce an array" - type not specified for output array
*
* Logic explanation:
*
* Assume we have array [a,b,c,d] Let multiple be multiple of each element
* in array. Hence multiple = 0 if one of the element is 0; To produce the
* output. Ans at i -> multiple divided by the value at i. if 2 numbers are
* 0 then entire output will be 0 because atleast one 0 will be a multiple
* if 1 number is 0 then every thing else will be 0 except that index whole
* value is to be determined
*
*/
public static BigInteger[] calcArray(final int[] inp) throws Exception {
if (inp == null)
throw new Exception("input is null");
int cnt = 0;
BigInteger multiple = new BigInteger("1");
boolean foundZero = false;
for (int i : inp) {
if (i == 0) {
cnt++;
foundZero = true;
if (cnt < 2)
continue;
else
break;
}
multiple = multiple.multiply(BigInteger.valueOf(i));
}
BigInteger ans[] = new BigInteger[inp.length];
for (int i = 0; i < inp.length; i++) {
if (foundZero) {
if (cnt < 2) {
ans[i] = (inp[i] == 0) ? multiple : new BigInteger("0");
} else {
ans[i] = new BigInteger("0");
}
} else {
ans[i] = multiple.divide(BigInteger.valueOf(inp[i]));
}
}
return ans;
}
}
But I was not selected. I would like to get feedback on my answer and see whats wrong.
Thank you.
i tried the same way in the past. didnt selected :) . I tried to captured the index of zero if any in first loop. and simply assigned the product to that index ( i used double array and default would be 0) - so you dont need to iterate again if one zero found.
And checked the product is infinity Double.isInfinite
in the first loop, if yes break the loop - no point of find product for remaining ( assume input is large numbers with high volume)
public static double[] arrayProducts(int[] input) {
int length = input.length;
double product = 1;
boolean zeroFound = false;
int zeroIndex = 0;
double[] output = null;
for (int i = 0; i < length; i++) {
if (input[i] == 0) {
if (zeroFound) {
throw new ProductArrayException(0, true, zeroIndex,
ZERO_FOUND_EXPECTION);
}
zeroFound = true;
zeroIndex = i;
} else {
product = product * input[i];
if (Double.isInfinite(product)) {
throw new ProductArrayException(0, true, zeroIndex,
INFINITY_FOUND_EXPECTION);
}
}
}
if (zeroFound) {
throw new ProductArrayException(product, false, zeroIndex,
ZERO_FOUND_EXPECTION);
} else {
output = new double[length];
for (int i = 0; i < length; i++) {
output[i] = product / input[i];
}
}
return output;
}