Facebook | Phone Screen | Product of Array Except Self [Rejected]

Hi All,

I wanted to share my experience from phone interview i had yesterday with Facebook. Interviewer joined a few mins late and gave a min intro on what his role was. He quickly got onto the Coder pad and pasted the problem statement

Question:
https://leetcode.com/problems/product-of-array-except-self

My Implementation
public int checkForZeros(int[] arr) {
    int total = 0;
    for (int i : arr) {
        if (i == 0) {
            total = total + 1;
        }
    }
    return total;
}

public int[] productArray(int[] arr) {
    if (arr.length == 1) return new int[0];
    int total = 1;
    int zeroCount = checkForZeros(arr);
    if (zeroCount > 1) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = 0;
        }
    } else if (zeroCount == 1) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 0) {
                total = total * arr[i]; //2
            }
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                arr[i] = total; //8
            } else {
                arr[i] = 0;
            }
        }
    } else {
        for (int i = 0; i < arr.length; i++)
            total = total * arr[i]; // 24
        for (int i = 0; i < arr.length; i++)
            arr[i] = total / arr[i];
    }
    return arr;
}

I explained to him the time and space complexity of the algorithm and also ran through all corner test cases.

Follow-up:
He said my implementation was correct but asked me if this can be done in any other approach as division operation is very expensive.

I hardly had 4-5 mins to come up with new approach and wasn't able to provide an alternate. He wrapped up the call with last 3 mins open for any questions. I asked him if he had 2 problems statements in mind for me before the interview started and he said No. He said it depended on the approach i followed. The follow up question was his second problem statement. We wrapped up the call.

I get an email today from the recruiter saying they are not moving forward with my profile. Upon enquiring the reason, He said the comments from the interviewer were "Speed, issues coming up with the correct solution and not being able to do the second question as you mentioned"

Comments (15)