Microsoft Interview Question






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

xor both the contents of the array

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

Good answer, but there are cases where this wouldn't work.
For instance: Take the arrays [0,3] and [1,2].
0(d): 00(b)
1(d): 01(b)
2(d): 10(b)
3(d): 11(b)
Now 0^3 = 3 and 1^2 = 3. (^ is bitwise xor)
So they xor to the same answer, but aren't the same arrays.

Or did you mean something else with your answer?

- soc October 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with Anonymous. Start off with zero; xor with all the elements in array 1; xor with all the elements in array 2. If you end up with zero, you have both array having same set of integers; otherwise not. Total O(n).

- Ananamas October 29, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This wont work....
Array 1:5,5
Array 2:6,6

- abhimanipal May 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we just add them and compare the sum of both array, as if both of them has same set of integer than sum should be same......

PS: Don't worry about overflow that can be handled in a program.......

- Kunal Gaind October 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope, you can most certainly have different arrays of numbers that add up to the same sum, can't you?
Eg: [1,2,3] and [2,2,2]

- soc October 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sum of the squares of the two arrays should be the same if the both the arrays have the same set of numbers .

- Subchin October 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A={0,5} ------ sum of squares 25
B={3,4} ------- sum of squares 25

that doesnt work

- Anonymous November 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is this? XOR, sum, sum of squares are all incorrect approaches. You could say that arrays are the same, when they actually are not.

Sorry to say this, but it seems like you guys need a course in basic math/logic.

- LOLer October 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you give a counter example to the XOR solution?

Thanks.

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

Counter Example: Consider an array with duplicates and the XOR result can be made zero.

- Mahesh October 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort each array using some fast algorithm like merge sort O(nlogn) and subtract corresponding element and put result in one of the two arrays. then scan that result array to find any non zero element. if found, it means they are not same.

- integralrootcosxdx November 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

read the question ... it says faster than nlogn

- Anonymous November 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#

- Code Monkey November 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use hashing....put numbers of array1 in hash....and go through numbers of array2 and check if it is in the hash....decrement count in hash if it's more than one element.. shud be O(n)

on a side note..this is set equality problem..and lower bound of set equality is O(nlogn) using any comparison based algorithm

- Anonymous November 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For ur god's sake, please read the question before bull crapping all over

- Abs November 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the sum of distinct numbers in both the arrays should be equal..

- Anonymous November 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Are you brain dead?

- Hammer November 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the sum of distinct numbers in both the arrays should be equal..

- ... November 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Give the correct answer ..I am not as smart as you guys ..

- Give the correct answer November 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

waitng for a better answre ... as i cant think of ne

- Anonymous November 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take product of all nos in both arrays See if products match
Take sum of all nos in both arrays See if sums match
If both match then the arrays may contain same ints

- Anonymous December 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Above point will fail if arrays are:
Arr1[0,1,2]
Arr2[0,3,0]
Both arrays have same sum and product (3 and 0)
But elements are not equal.

- girish December 28, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Add the elements of the first array in a binary tree. Now for every element in array 2, we try to insert in binary tree. In our binary tree we check the condition if the value already exists we, simply delete the node from the binary tree.
Thus, if the 2 arrays have the same elements then, at the end of the process the binary tree would be empty

- kunal.karoth March 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

binary tree or binary search tree? build a binary search tree requires O(nlogn).

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

If question is => all integers in Arr1 are in Arr2 with same frequency. Then we can check for the following condition for success =>
if size[Arr1] == size[Arr2] and Arr1[0...n] XOR Arr2[o....n] == 0 and Sum(Arr1[0...n]) == Sum(Arr2[o....n])

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

is any body to take it further and clarify the questions...

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

We take the sum of the differences of each corresponding element, this must be zero for equality of the arrays.
ie a[0]-b[0]+a[1]-b[1]+...+a[n]-b[n] = 0

We take the sum of each array. This must also be same for equality.
ie a[0]+a[1]+...+a[n]=b[0]+b[1]+...+b[n]

Please cross-check. I haven't worked out its correctness.

- droy September 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is sooo stupid.
The "two" equations you have given are same.

Here is your counter example
4 8 3 12
0 1 2 24

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

I have one approach which runs in o(n) without extra space but valid only when you know the upper limit of number.
Suppose you are given that the number lies within 1-n where n is the size of array then you can easily find in o(n) by iterating a loop ar[abs(ar[i])%n]+=n;
Ex: ar1[]={2,3,1,4} then ar1 after loop becomes { 6,7,5,8}
ar2[] ={1,4,2,3} then ar2 becomes { 5,6,7,8} so in one loop you can easily count the number of times each index comes by diving by n and then checking elements in both arrays.

- Deepak Agrawal April 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simply XOR and take the sum of squares of the elements

Both should match

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

Simply XOR and take the sum of squares of the elements

Both should match

- Subodh December 31, 2015 | 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