Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

left_partial = 1;
right_partial = 1;
for (i = 0 ; i < n; i++)
{
prod[i] = left_partial;
left_partial *= arr[i];
}

for (i = n - 1; i >= 0; i--)
{
prod[i] *= right_partial;
right_partial *= arr[i];
}

O(n) time complexity
O(1) space complexity

- Anonymous September 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Gud One

- learner September 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Amazing Solution!

- Muhammed.Adly September 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome...

- Krishna October 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Gud solution.

- Angamuthu G October 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How is this algo O(n). 2 for loops going from 0-n makes it O(n2)

- NewGuy October 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@newguy...it will make 2n not n2.......one loop is not inside another....!

- Kamlesh Meghwal October 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this sol rocks.... thanks

- srik545 January 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome Solution !

- Abhishek February 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice one...

- Lucky March 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well thought ... :)

- Anonymous August 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could someone please tell what could be the input and expected output for easy understanding of this question?

- Anonymous October 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome solution!!

- Anonymous December 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

very good solution......

- kathrotiyanimesh May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
6
of 6 vote

if element stored in int e[];
go from begin to tail, h[i]=h[i-1]*e[i]
go from tail to head, t[i]=t[i+1]*e[i]
then the result is prod[i]=h[i-1]*t[i+1]

- xq September 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome.. works just fine!

- anmolkapoormail September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice answer but though the space complexity is O(n)

- anshulzunke September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can be done in-place


for (int i = 0; i < l; i++) {
partial_prod_start *= arr[i];
partial_prod_end *= arr[l - 1 - i];

if (i + 1 < l) prod[i + 1] *= partial_prod_start;
if (l - 1 - i -1 >= 0) prod[l - 1 - i -1] *= partial_prod_end;
}

- Anonymous September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

gud one

- just_a_thought September 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int main()
{
int i,j,pro=1;
int prod[5],arr[10]={1,2,3,4,5};
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
if(i==j)
continue;
else
pro=pro*arr[j];
}
prod[i]=pro;
pro=1;
}
for(i=0;i<5;++i)
printf("%d\t",prod[i]);
printf("\n");
return 0;
}

- Srimanyu February 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int main()
{
int i,j,pro=1;
int prod[5],arr[10]={1,2,3,4,5};
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
if(i==j)
continue;
else
pro=pro*arr[j];
}
prod[i]=pro;
pro=1;
}
for(i=0;i<5;++i)
printf("%d\t",prod[i]);
printf("\n");
return 0;
}

- Srimanyu February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Srimanyu.. nice one! i thought the same! simple but do you think one can reduce the time complexity from O(sq(n)) to O(n)?

- RohitHomiCiDe July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Multiply all the numbers in one variable and than traverse the array and divide the product by the i-th to store i-th product value using division without using the division operator.
Division can be done using bit shifting method.

- raj.nsit1 September 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

workable idea... good one!

- anmolkapoormail September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sir please show me any method for performing division by bitwise operators

- sangee October 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int prod =1;
for(int i=0;i<n;i++)
prod = prod*arr[i];

for(int i=0;i<n;i++)
prod[i] = prod/a[i];



is this solution acceptable?

- shyam February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no its not. problem says you should not use / operator

- sg February 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no its not. problem says you should not use / operator

- sg February 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if the integer array contains a zreo? You cannot use divide in this case.

- marketbuzz June 12, 2012 | 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