Zocdoc Interview Question: Given an integer array, write... | Glassdoor

Interview Question

Senior Software Engineer Interview New York, NY

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.
Answer

Interview Answer

5 Answers

2

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();
}

Interview Candidate on Jun 9, 2014
1

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

h on Jul 28, 2014
0

This is not O(n). Your product() function alone is O(n). This is actually O(2n).

Anonymous on Sep 25, 2014
1

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

Anonymous on Apr 5, 2015

This post has been removed.
Please see our Community Guidelines or Terms of Service for more information.

Add Answers or Comments

To comment on this, Sign In or Sign Up.