Microsoft Interview Question for Software Engineer / Developers






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

Multiply all the elements and create a master product value. Then traverse the array and just divide the master product with the element in the corresponding spot and store it. This is linear time.

- Vivek March 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if A contains a 0

- DH March 19, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

then it's even more simpler. in list B, except for the entry at which the zero is present, all other entries will be zero. for the entry at which zero is present, just multiply the other entries. if there are more than one zeros in list A, then all entries in list B are zeros.

- Raman March 25, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution.

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

We can calculate the array B in the following manner.

1. First iterate thru Array A (from i=1 to n) and for each value of A[i] store the product of A[1] ..A[i-1] elements in B[i].
2. Next iterate thru array A (from i=n to 1) and for each value of A[i] store the product of A[i-1] .. A[n] in B[i].
3. After the steps 1 and 2 we will have the required values in array B

code for the same is as follows:

void foo(const int A[], const int len)
{
	int B[len];
	int prod = 1;
	int i=0;

	for(i=0;i<len;i++)
	{
		B[i] = prod;
		prod *= A[i];
	}
	prod = 1;
	for(i=len-1;i>=0;i--)
	{
		B[i] = prod;
		prod *= A[i];
	}

	for(i=0;i<len;i++)
	{
		printf("%d\t", B[i]);
	}
}

- algooz April 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

oops there was bug in the code

void foo(const int A[], const int len)
{
   int B[len];
   int prod = 1;
   int i=0;

   for(i=0;i<len;i++)
   {
      B[i] = prod;
      prod *= A[i];
   }
   prod = 1;
   for(i=len-1;i>=0;i--)
   {
      B[i] *= prod;
      prod *= A[i];
   }

   for(i=0;i<len;i++)
   {
      printf("%d\t", B[i]);
   }
}

- algooz April 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A small optimization:
If the numbers are too large, product for all of them may overflow. We can keep product of (n-1) first and hen traverse. For each element first divide the product by a[i] and then multiply by a[i-1].
As for zeros are concerned, we can handle them separately.

- varun_agg June 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

long * product (int *a, int n)
{
int i, count =0;
long product = 1;

//if empty string..
if(n==0)
return NULL;

//Allocate the memory which is initialized to zero...
int *p = calloc(n, sizeof(int));

//Find the product
for(i=0; i<n; i++)
if(a[i] == 0)
count++; //Count number of zeros
else
product = a[i]*product;

if(count > 1) //Everything is zero.. juz return the p
return p;

for(i=0; i<n; i++)
if ( a[i] == 0)
p[i]=product;
else
p[i]=product/a[i];

return p;

}

- coder September 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nobody though about the overflow???

So to avoid this condition, one must check if the multiplication would lead to the overflow condition. If it does, one should simply divide the product by the number we have while iterating and hence in the worst case it may lead to O(n2) complexity.

One should also ask that even after division, if there is overflow what should be done.

- AD September 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Working code!!

main()
{

int i,l,r,x[5]={5,3,4,1,2},y[5]; // x is the initial array, y is the final array

int n = 5; // n be the size of the array, here already defined as 5

l = 1; // l is the product of the values of the left side of an index in x
r = 1; // r is the product of the values of the right side of an index in x

for (i=0 ; i<5 ; i++) y[i]=1; // initialising all y values to 1

for (i=0 ; i<5 ; i++)
{
y[i] = y[i]*l ;
y[n-i-1] = y[n-i-1]*r;

l = l*x[i];
r = r*x[n-i-1];

}

for (i=0; i<5; i++) printf("%d\n",y[i]);
getch();
}

- Nikunj October 13, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void arraymultiple(int *input,int *output,int size)
{
long int result=1;
int i;
for(i=0;i<size;i++)
{
output[i]=result;
result*=input[i];
}
result=1;
for(i=size-1;i>=0;i--)
{
output[i]*=result;
result*=input[i];
}
}

- rajnesh November 11, 2008 | Flag Reply


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