Microsoft Interview Question






Comment hidden because of low score. Click to expand.
0
of 0 vote

If I recall correctly, in this problem, division was not allowed.

- Anonymous March 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then why did not you tell that in the problem description? You can not miss out such important information.

- russoue February 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would like to know if I have understood the question correctly.

Input and Index are two integer arrays each of size n.For example if n=5 the size of each array is 5.

Let us say the input array contans following elements 3,5,6,7,8 that is input[0]=3 input[1]=5 input[2]=6 input[3]=7 input[4]=8

similarly index array is also populated.
index[0]=18 index[1]=4 index[2]=30 index[3]=5 index[4]=10

considering that result[i] is product of all elements in input except the element at input[index[i]].Now when i=0 i have index[0]=18 i dont have an element at input[18] as the size is 5,so will the product be all elements from input[0] till input[4] =5040.

and when i=1 index[1]=4 hence input[4] is excluded and the product of the rest is caculated ie 3*5*6*7=630

- anonymous March 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's a valid point. But here i think we should assume that each of the two input arrays contain 1-n numbers.

- vinay March 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does it makes sure that input array does not go out of limit(i cant use index word here:D) so question should provide that index array should contain numbers between 0 and n-1.

- sanjeev April 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for (int i = 0; i != n; ++i) {
    result[i] = 1;
}

for (int i = 0; i != n; ++i) {
    if (index[i] != i) {
        result[i] *= input[i];
    }
}

Would this work?

- cristi.vlasceanu May 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesn work cuz each result[i] must have the product of all the inputs except input[index[i]]. and this code doesnt.

I cannot think of a linear solution without dividing.

- VK July 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right VK, I did not think this thru. Here's another shot at it:
For each i, we compute two numbers: the products of all numbers from input[0] to input[i], and the products from input[i] to input[N - 1].
Then, for each i, if j = index[i], the result is to[j - 1] times from[j + 1]. There might be bugs in the code below but I think the principle is correct. Thoughts?

#include <iostream>
using namespace std;

#define N 5

int input[] = { 1, 2, 3, 4, 5 };
int index[] = { 0, 1, 2, 2, 2 };
int result[N];
int to[N]; // partial products from [0 to i]
int from[N]; // partial products from [i to N)

int main()
{
    for (int i = 0; i != N; ++i)
    {
        result[i] = 1;
    }
    for (int i = 0; i != N; ++i)
    {
        to[i] = input[i];
        if (i)
        {
            to[i] *= to[i - 1];
        }
    }
    for (int i = 0; i != N; ++i)
    {
        from[N - i - 1] = input[N - i - 1];
        if (i)
        {
            from[N - i - 1] *= from[N - i];
        }
    }
    for (int i = 0; i != N; ++i)
    {
        int j = index[i];
        if (j)
        {
            result[i] = to[j - 1];
        }
        if (j < N - 1)
        {
            result[i] *= from[j + 1];
        }
        cout << result[i] << endl;
    }
    return 0;
}

- cristi.vlasceanu July 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thats a nice soln assuming extra space.

- VK July 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

really nice

- AB December 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

well done

- Anonymous January 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excellent!

- russoue February 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

great job..

- Anonymous February 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just another way of doing this. Create an array v2 whose elements are the product of all elements of v1 except the element at that index. Then re-arrange as required by the index array. Takes 3 parses of O(n).

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void transform (int* v1, int* index, int* v2, const int& size) {
int p = 1;

for (int i = 0; i < size - 1; ++i) {
p *= v1[i];
v2[i+1] = p;
}

p = 1;
for (int i = size - 1; i > 0; --i) {
p *= v1[i];
v2[i-1] *= p;
}

for (int i = 0; i < size; ++i) {
swap(v2[index[i]], v2[i]);
swap(index[index[i]], index[i]);
}
}

int main() {
int v1[] = {1,2,3,4,5};
int v2[] = {1,1,1,1,1};
int index[] = {4,2,1,3,0};

transform(v1, index, v2, 5);

copy(v2, v2 + 5, ostream_iterator<int>(cout, " "));
cout << endl;

return 0;
}

- someguy November 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) algorithm with O(1) memory. This algorithm traverses both the arrays twice, once calculation product of numbers on the left and then calculating products on the right side. The result of this traversals is written in the results array.

int[] arr1 = { 1, 2, 3 };
                int[] arr2 = { 10, 20, 30 };
                int[] output = new int[arr1.Length];

                int leftProd = 1;
                for (int i = 0; i < arr1.Length; i++)
                {
                    output[i] *= leftProd;
                    leftProd *= arr1[i];
                }

                int rightProd = 1;
                for (int i = arr1.Length - 1; i >= 0; i--)
                {
                    output[i] *= rightProd;
                    rightProd *= arr1[i];
                }

                leftProd = 1;
                for (int i = 0; i < arr2.Length; i++)
                {
                    output[i] *= leftProd;
                    leftProd *= arr2[i];
                }

                rightProd = 1;
                for (int i = arr2.Length - 1; i >= 0; i--)
                {
                    output[i] *= rightProd;
                    rightProd *= arr2[i];
                }

                
                output.ToList().ForEach(Console.WriteLine);

- anunay July 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

which array is index, arr1 or arr2?

- cwr July 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The original problem statement is not clear. Index is not an array. It is the current index of the array. The input is just two arrays. In the example I posted above index value is i for the current array we are processing.

For better a explanation to a slight variation of this problem please see http:_geeksforgeeks.org/?p=7527

- anunay July 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, my bad I reread the original problem. I interpreted it incorrectly. The solution I posted above is incorrect, but it is still possible to solve this in O(n) algorithm with O(1) memory, by slightly modifying the solution given in the following post geeksforgeeks.org/?p=7527

- anunay July 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another shot at the problem.

public void ProductOfArrayElements()
{
    int[] inputArr = { 1, 2, 3 };
    int[] indexArr = { 2, 0, 1 };
    int[] output = new int[inputArr.Length];


    int leftProd = 1;
    for (int i = 0; i < inputArr.Length; i++)
    {
        output[ i ] = leftProd;
        leftProd *= inputArr[indexArr[i]];
    }

    int rightProd = 1;
    for (int i = inputArr.Length - 1; i >= 0; i--)
    {
        output[i] *= rightProd;
        rightProd *= inputArr[indexArr[i]];
    }

    output.ToList().ForEach(Console.WriteLine);

    // Output is [2, 6, 3 ]
    // 2 = 1 * 2  ( skipping inputArr[2])
    // 6 = 2 * 3  ( skipping inputArr[0])
    // 3 = 1 * 3  ( skipping inputArr[1])
}

- anunay July 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey78619" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}
getpro(int arr[],int n,int out[])
{
int prod=1,x=1;
for(int i=0;i<n;i++)
prod=prod*arr[i];

for(i=0;i<n;i++)
{
x=1;
while(x*arr[i]!=prod)
x++;
out[i]=x;
}
}

</pre><pre title="CodeMonkey78619" input="yes">what is the time complexity for dis code??? somebody help</pre>

- Anonymous July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If I understand it correctly, then the function should find product of the entire array elements of input except the element at particular index in the index array:

long mul = 1;
for (i=0 to n)
mul*= input[i];

for(i=0 to n){
result[i] = mul / input[index[i]];
}

- Anonymous March 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is no condition to check input[index[i]] is actually had value or not. That means we can verify index[i] is <= n-1. So that this will work very well.

- Anonymous April 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@above algorithm fails, if input array contains zero value

- Adnan Ahmad September 23, 2013 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More