Microsoft Interview Question for Software Engineer in Tests






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

Take XOR of binary equivalents of all the numbers in 2 arrays, then complement the result and get its decimal equivalent and you have the answer :)

- Anirvana August 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You really should think before writing and confusing people's minds.

At least check your method with a simple example.
In your case, even without checking with an example,
one can find out that this is INCORRECT since
you are supposed to find the element in the first array that is NOT in the second array. So the result should be independent of the element(s) which is in the second array but NOT in the first array. Please tell me, by XOR'ing every element in both arrays, how can that be satisfied?

- erm October 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The XOR solution, as Anirvana mentioned, works perfectly for this problem with following assumption:

1. no duplicates elements in both array.
	2. only one element is missing 
	3. the missing element is only replace by 0;

the sample inputs meet all these 3 assumptions.
And here is my code:

int Find_Missing(vector<int> &array1, vector<int> &array2)
	{
		int xor = 0;
		vector<int>::size_type i;
		for (i =0; i < array1.size(); ++i)
		{
			xor ^= array1[i];
		}
		for (i =0; i < array2.size(); ++i)
		{
			xor ^= array2[i];
		}
		return xor;
	}

But I am not going to tell you why it works, since generally you should be polite especially when you don't know something.
And I also like to cite your words:

"You really should think before writing and confusing people's minds. "

- yyang.even November 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome solution! I don't think you need to take compliment of the final xor though...taking xor of all the elements of the two arrays adhering to the constraints mentioned by yyang.even gives the correct output

- rahul.goswami0206 January 28, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Take the sum of all the elements of the first array in O(n)
2. Take the sum of all the elements of the second array in O(n)
3. Subtract the bigger one from the smaller one and then thats the number.

- Pallav August 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if the 0 in the second array was 6 or better yet 60000000000000?

- jctechsan August 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What about if there are no numbers match. Example: array1 = {1, 2, 3, 4, 5} and array2 = {6, 7, 8, 9, 10}. I guess the Triton's answer is correct.

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

Quesion says "which" number "is" => it is talking about single number. Hence Pallav is correct.

@jctechsan :
Take mod of the difference.

- cirus September 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry : take absolute value of difference

- cirus September 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@cirus how will you justify about this case
A = {1,2,3,4,5}
B = {2, 3, 1, 0, 5, 5}
?

- pankaj January 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it's the problem with 2n+2 elements where 2n are pairs and 2 different. one can find the two numbers in O(n) time and O(1) space using xor trick.
geeksforgeeks.org/?p=2457

- S August 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort both arrays. Use 2 pointers, move them by 1 each time till Pointer1 value != Pointer2 value. That is the missing number

- Metta August 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorting will take o(nlogn) and you are considering that size of 2nd array is equal to size of 1 ...if not then in this case your method will not work

- ashish.cooldude007 August 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

but if memory is a concern(no hashtable):
1.then sort arr1(index is i) and arr2(index=j)
2. while (i<arr1.length && j< arr2.length)
if arr1[i]== arr2[j] then i++, j++
if arr1[i]>arr2[j] then j++
if arr1[i]<arr2[j] then print arr1[i] and i++;

time complexity: O(nlogn+mlogm+m or n)

Please comment

- Anonymous August 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry frgt to mention my name for the above code

This can be used for as many non-matching numbers in array2

- satish August 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry frgt to mention my name for the above code

This can be used for as many non-matching numbers in array2

- satish August 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort array2
Now binary search for every element in array1

Time complexity : N(log M) + MLogM => (N+M)logM
N = no of elements in array1
M = No of elements in array2

- DashDash August 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution, But I am not sure the process time.

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

if only one no is missing simply xor both array else mk a hash n chk

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

Have a single hash for both arrays, initially the values are 1 for the first array. When you put the 2nd keys into the array increment the count. If the count in the hash table is still 1.. The number is not found in the 2nd array..

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

if only one number is missing then there is no problem at all just take the xor of all the elements and atlat the answer u will get but ifthe scenario is different then
u have to sort theses arrays an then see
like it will take the 0(nlgn) solution :
:)

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

1. Take the first array as row, we name this array as A,
2. The second one as column, we name this array as B
3. we can make an m*n matrix S, S[i][j] is equal to 1 if A[i] = B[j], for giving two arrays, we can make following matrix:
0 0 1 0 0
1 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 1
5. check each row to see if it doesn't contain 1, the element in Array A corresponding this row is not present in the second array

- James September 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package test;

import java.util.HashSet;
import java.util.Set;

public class ValueNotPresentInsecondArray {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]={1,2,3,4,5};
int b[]={2,3,1,0,5};
Set temp=new HashSet();

for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
temp.add(a[i]);
System.out.println(a[i]+" present");
}

}
}
for(int i=0;i<a.length;i++)
{
if(!temp.contains(a[i])){
System.out.println(a[i]+"does not present");
}
}


}

}

- Anonymous July 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1 present
2 present
3 present
5 present
4does not present

- damu July 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<conio.h>
#include<iostream.h>
void main()
{int i,j,n,a[10],b[10],flag=1,c;
clrscr();
cout<<"enter number of elements\n";
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"enter elements for the second array";
for(i=0;i<n;i++)
cin>>b[i];
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ if(a[i]!=b[j])
flag=0;
}
c=a[i];
break;
}
if(flag==0)
cout<<"element not present in the second array is "<<c;

getch();
}

- Anonymous July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<conio.h>
#include<iostream.h>
void main()
{int i,j,n,a[10],b[10],flag=1,c;
clrscr();
cout<<"enter number of elements\n";
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"enter elements for the second array";
for(i=0;i<n;i++)
cin>>b[i];
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ if(a[i]!=b[j])
flag=0;
}
c=a[i];
break;
}
if(flag==0)
cout<<"element not present in the second array is "<<c;

getch();
}

- Raghav July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i simple solution if there aren't any duplicates and you are finding the unique element in the first array

public static void main(String[] args) {
int[] a = new int[] { 1,2,3,4,5,7 }; // {1,3,5,7,11}

int[] b = new int[] { 2,3,1,0,5 }; // 19 {1,3,5,9}
findNumber(a, b);
}

public static void findNumber(int[] a, int[] b) {
Arrays.sort(a);
Arrays.sort(b);

int n1 = a.length;
int n2 = b.length;
int temp = -1;
int i = 0;
int j = 0;
while (i < n1 && j < n2) {
if (a[i] > b[j]) {
j++;
} else if (a[i] < b[j]) {
System.out.print(a[i] + " ");
i++;
} else {
i++;
j++;
}
}

while (i < n1) {
a[i] = a[i];
System.out.print(a[i]);
i++;
}

}

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

#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={1,2,7,4,6},b[]={2,3,0,1,5};
int i,j,k;
int flag=0;
clrscr();
for(i=0;i<5;i++){
for(j=0;j<5;j++){
if(a[i]==b[j]){
b[j]=b[j+1];
b[j+1]='\0';
}
}
}
for(i=0;i<5;i++)
{
if(b[i]!=0){
printf("%d",b[i]);
flag++;
}
}
if(flag==0)
printf("%d",flag);
getch();
}

- vigneshwaran December 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={1,2,7,4,6},b[]={2,3,0,1,5};//we can give any distinct value to a or b,
int i,j,k;
int flag=0;
clrscr();
for(i=0;i<5;i++){
for(j=0;j<5;j++){
if(a[i]==b[j]){
b[j]=b[j+1];
b[j+1]='\0';
}
}
}
for(i=0;i<5;i++)
{
if(b[i]!=0){
printf("%d",b[i]);
flag++;
}
}
if(flag==0)
printf("%d",flag);
getch();
}

- vigneshwaran M December 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

visit first array push all the numbers into the hash..
visit second array check if they exist in hash...

- Crime_Master_GoGo August 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

0 not in the hash,will u return it?
they want those in array 1 while not in array 2
NOT
those in array2 not in array 1

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

Its actually the opposite, put the elements of the second array in the hash table and for every element of the first array, check whether its present in the hash or not, O/P all those elements from the first array that are not present in the hash table.

- Triton August 05, 2010 | 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