Zocdoc Interview Question

Given an integer array, write a program that returns an array with elements = product of the integers in input array except the one in its position. Ex: Given input: [2, 3, 10, 4, 5], output: [600, 400, 120, 300, 240] What is the complexity of your program? When will your program not work? Below is what I presented (O(n)), but it did not qualify me for the next round.

Interview Answers

Anonymous

Jun 9, 2014

public int product(List input) { int product = 1; foreach(int i in input) { product = product * i; } return product; } public void printOutput(List input) { foreach(int i in input) Console.WriteLine(i); } public List multiply(List input) { List output = new List{}; int total_product = product(input); foreach(int i in input) { output.Add(total_product/i); } return output; } public void main() { List input = new List{"2","3","10","4","5"}; printOutput(multiply(input)); Console.ReadLine(); }

2

Anonymous

Jul 29, 2014

If the pre condition is none zero integer list then I think the answer is correct.

1

Anonymous

Apr 5, 2015

While the above solution does iterate the list twice, that doesn't matter for asymptotic runtime - it's still O(n).

1

Anonymous

May 22, 2018

I've seen the same question before but it's usually asked without using a division. You have to iterate forward once, and then backwards one more time O(2N) => O(N).