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 answerimport 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 what's wrong.
Array whose values are the product of every other integer
java;array;interview questions
It might not because of the code. Maybe you were unlucky and there was a better candidate or a candidate with more experience in their industry.If it's not a bottleneck in an application (or a library function) don't do premature optimization. The naive solution is much easier to understand, faster to develop and easier to maintain since it's not so complex:public static BigInteger[] calcArray2(int[] input) throws Exception {    if (input == null) {        throw new NullPointerException(input is null);    }    BigInteger result[] = new BigInteger[input.length];    for (int i = 0; i < input.length; i++) {        result[i] = calculateProductExcept(input, i);    }    return result;}private static BigInteger calculateProductExcept(int[] input, int exceptIndex) {    BigInteger result = BigInteger.ONE;    for (int i = 0; i < input.length; i++) {        if (i == exceptIndex) {            continue;        }        result = result.multiply(BigInteger.valueOf(input[i]));    }    return result;}Anyway, is n division faster than n*n multiplications?More or less related: YAGNI, Simplicity -- the art of maximizing the amount of work not done -- is essential from the Agile ManifestoYou have a lot of copy pasted code here: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 })));You could extract out a method:private static void printProductOfArray(int... input) throws Exception {    System.out.println(Arrays.toString(ProductOfAnArray.calcArray(input)));}Swallowing exceptions is not a good practice, even in an exercise:} catch (Exception e) {    // debug exception here and log.}If something throws an exception you'll miss it. You could modify the main method to throw exceptions and remove the try-catch block instead:public static void main(String[] args) throws Exception {(The Pragmatic Programmer: From Journeyman to Master by Andrew Hunt and David Thomas: Dead Programs Tell No Lies.)You could throw a more specific exception here, like an IllegalArgumentException or a NullPointerException:if (inp == null)    throw new Exception(input is null);Calling the method with null input array is rather a programming error, so unchecked exceptions are better here. (It would make the throw Exception on the main unnecessary.)I guess result (as variable name) would be easier to understand here:BigInteger ans[] = new BigInteger[inp.length];I'd also avoid abbreviations. I've found that they undermine autocomplete. (When you type co and press Ctrl+Space in Eclipse it founds nothing but it could found count.) Furthermore, they require mental mapping (readers/maintainers have to decode them every time.See also: Clean Code by Robert C. Martin, Use Intention-Revealing Names, p18; Use Pronounceable Names, p21; Avoid Mental Mapping, p25I'd rename this variable to fullProduct or something more descriptive to express its purpose:BigInteger multiple = new BigInteger(1);