Microsoft Interview Question


Country: -




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

the missing number = xor all numbers from both stacks

- Anonymous October 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think adding the contents of both the stacks and subtracting one frm the oder can be one soln..

- ANONU October 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Adding numbers is probably fastest if only 1 number is missing.
Worse ways:
Sort 2 stacks then walk them.
Hash the numbers in the smaller stack then walk the other one checking hash. (If same number is in there twice you mark it in hash table as number 2 then while walking you substract 1 each time you find that number. When you don't find it in hash, that is the number.

- Ninja Coder October 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes xor will do the work and is faster than adding and hashing

- Algo Visa October 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have a question,tht if the number r not in order how xor will help ..plz give a algo

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

Xor is commutative. a-xor-b = b-xor-a.

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

can any1 give d algo..bcoz i didnt get it properly.??thanx in advance.

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

1. a^a = 0
2. a^0 = a

So xor even number of any integer results zero, and xor odd number of any integer results the number itself. When putting all numbers from both stacks together, every number, but the missing one, appears twice. What left is the single appearance missing number.

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

The problem has two cases:

1. If only 1 element is missing:

Here do the XOR of all the elements and the ans will be the missing one.

2. If more than 1 element are missing:

Use the method of hashing or sort the stack in place and do the comparision.

- Nascent Coder October 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi can anyone provide a small example? I did not understand the algorithm, yet.
Thanks

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

#include "stdio.h"

int main()
{
int stack1[]={10,12,13,14};
int stack2[]={12,10,14};
int size1= sizeof(stack1)/sizeof(stack1[0]);
int size2= sizeof(stack2)/sizeof(stack2[0]);
int missednumb=0;
int i=0;
if (size2<size1)
{
while(i<size2)
{
missednumb^=stack1[i]^stack2[i];
i++;
}

}
else
{
while(i<size1)
{
missednumb^=stack1[i]^stack2[i];
i++;
}
}
if (size2<size1)
{
missednumb^=stack1[i];
}
if (size1<size2)
{
missednumb^=stack2[i];
}
printf("\n The Missed number is %d",missednumb);
return 0;
}

- Anonymous December 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for both stack s1,s2

pop elements of s1 first
-maintain a separate negativesum and a positivesum(sum of negative & positive no.s respectively);
-lets say they r ns1 &  ps1
similarly for s2
find ns2,ps2

now 
if(ns1==ns2) { if(ps2>ps1) missingno=ps2-ps1; 
                        else missingno=ps1-ps2;
                      }
else if(ps1==ps2) { if(ns2>ns1) missingno=ns2-ns1; 
                        else missingno=ns1-ns2;
                      }

- codez July 04, 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