Interview Questions

find out missing no. from a stack if two given stacks have same numbers .

Microsoft Interview Questions and Answers


(Continued from previous question...)

24. find out missing no. from a stack if two given stacks have same numbers .

Question:
find out missing no. from a stack if two given stacks have same numbers


maybe an answer1:

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.


maybe an answer2:

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.

(Continued on next question...)

Other Interview Questions