Amazon Interview Question for Software Engineer / Developers


Country: India




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

Taken from geeksforgeeks

#include <stdio.h>;
#include <stdlib.h>;
 
/* This finction sets the values of *x and *y to nonr-epeating
 elements in an array arr[] of size n*/
void get2NonRepeatingNos(int arr[], int n, int *x, int *y)
{
  int xor = arr[0]; /* Will hold xor of all elements */
  int set_bit_no;  /* Will have only single set bit of xor */
  int i;
  *x = 0;
  *y = 0; 
 
  /* Get the xor of all elements */
  for(i = 1; i < n; i++)
   xor ^= arr[i];
 
  /* Get the rightmost set bit in set_bit_no */
  set_bit_no = xor & ~(xor-1);
 
  /* Now divide elements in two sets by comparing rightmost set
   bit of xor with bit at same position in each element. */
  for(i = 0; i < n; i++)
  {
    if(arr[i] & set_bit_no)
     *x = *x ^ arr[i]; /*XOR of first set */
    else
     *y = *y ^ arr[i]; /*XOR of second set*/
  }
}     
 
/* Driver program to test above function */
int main()
{
  int arr[] = {2, 3, 7, 9, 11, 2, 3, 11};
  int *x = (int *)malloc(sizeof(int));
  int *y = (int *)malloc(sizeof(int));
  get2NonRepeatingNos(arr, 8, x, y);
  printf(" The non-repeating elements are %d and %d";, *x, *y);
  getchar();
}

- Rajat Rastogi February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can anyone explain the algorithm behind this logic?

- hello world February 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

very good logic....

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

I think it works fine !! A very simple logic in fact.

Now, consider the example:
{1,1,3,5} the binary representation is:
{001,001,101,110}
The XOR value will then be: 011
On dividing the numbers based on the right most bit value set in XOR, the 2 groups are:
{1,1,3} and {5}
{001,001,101} and {110}
the x and y would then be:
x={101} y={110} x=3 and y=5
Hence it works

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

I thought the binary of 3 was 011 and that of 5 was 101.

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

Liked the solution.. especially segregating the sets using the righmost non-zero bit.

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

the example in the 3rd reply is total wrong

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

great logic..

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

Nice solution...

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

Awesome solution!!!!

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

How about you try this array as input {2, 2, 4, 4, 6, 8}

I get 6 and -857853060.

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

Lol..to first comment.. "Algorithm behind this logic!!"

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

So does this solution rely on the assumption that given the XOR of 2 numbers, there's only 1 pair of unique numbers which can lead to this XOR? If so, I can produce many pairs of numbers with the same value of XOR.

Furthermore, the code is weird because while it works for some cases, it won't work for others, depending on what duplicates we have, as well as the 2 non-repeating numbers of course. Has anyone tried verifying that the code actually work by setting different pairs of numbers as the non-repeating ones?

- Sunny December 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
5
of 5 vote

I am going to try explain this more clearly.

First how xor works: if you have two numbers say 10 (1010) and 8 (1000), xor operation ( done using operator ^ ) will return the bits which are set in one number but not in the other.

10 -- 1010
08 -- 1000
---------------
^ -- 0010
---------------
Here the third bit is set(from right) in 10 but not in 8;

Now the problem states that there are 2n + 2 numbers such that n numbers are repeated.
eg. for n = 3: { 2, 3, 2, 4, 5, 4, 5, 6 }
there are two of ( 2, 4 & 5 ) but only one of ( 3 & 6 )

Lets take the first set of number that repeats..
when we xor a number with itself the result is zero.
2 ^ 2 = 0 (in binary: 10 ^ 10 )
4 ^ 4 = 0
5 ^ 5 = 0

Now we have actually got rid of the numbers that repeats twice.

so when we xor all the elements of the array : a[i] ^ a[i+1] ^ ... a[n], we would have performed, for the above example ( 2 ^ 3 ^ 2 ^ 4 ^ 5 ^ 4 ^ 5 ^ 6 )
Since the repeating elements are nullified :) we are left with ( 3 ^ 6 ), the xor of the two elements we need to find. If the elements are equal, the 2n+2 constraint is not valid and hence these two numbers would have also been nullified.

Now all we have to do is extract the two numbers (x, y) from ( 3 ^ 6 )

3 ^ 6 is
011
110
-----
101
-----
what we can infer from the above is that the last bit is set in one number and not in the other. ( and so does the first bit)

set_bit_no = xor & ~(xor-1);
This line in the above example by Rajat Rastogi can be replaced with

int set_bit_no = 1;
while ( ! ( xor & set_bit_no ) )
set_bit_no <<=1;

what it does is right shift, a bit at a time and check whether it is set in the result of ( 3 ^ 6 )
Why do we need this?

Because, when doing a xor, a bit is set only when it is set in one and not in the other. So we are actually isolating a trait of one number so that we can extract it from the result.

Now is the tricky part.

for(i = 0; i < n; i++)
  {
    if(arr[i] & set_bit_no)
     x = x ^ arr[i]; /*XOR of first set */
    else
     y = y ^ arr[i]; /*XOR of second set*/

}

Our example array: { 2, 3, 2, 4, 5, 4, 5, 6 }
x & y is initially zero;

Again we apply the same principle, but this time we check whether the rightmost bit in set in one each element of the array..if its set, it will be removed when a pair of the same item is encountered and thus we will be able to extract x;

Then we also xor the elems which does not have the right most bit set. So, we will be extracting all the pairs with this bit not set and y ( the pairs cancel each other and y remains )

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

Great explanation. Very well explained!!!!

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

Great explanation. Very well explained!!!!

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

3^6 == 2^7
how will your algo solve this??

- aaa.ashu19 April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

3^6 == 2^7
how will your algo solve this??

- aaa.ashu19 April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

3^6 == 2^7
how will your algo solve this??

- aaa.ashu19 April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have one other solution for it -

We can use hash to solve it. Here we have to choose the hash function carefully..

find_duplicate()
{
let i = 0
compute x = hash(a[i])
if(a[i] == a[hash])
{ i++ }
else
{ swap(a[0],a[x]) }
if (i <= n)
{ call find_duplicate(i) }
else
{ return i}
}

This will find the element required for the first case.
If we now run the same algo for another n times it will find the 2nd element too.
I do not believe it to be faster than Xor algo. It's beautiful.

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

It runs in 0(n) time .. infact n and 2n times.

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

one correction - that is not a[hash] its a[x] where x = hash(a[0])

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

Sometime, recursion is not considered as constant space, as it will store n value of 'i' in stack.

- Sid February 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

God like...

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

Hi Joe,
Just trace the whole program, its smart and easy. You will understand it better.

- anjum February 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could someone explain about set_bit_no concept. Its not very clear to me.

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

Finding the exor all all elements gives a^b (where and b are the required numbers). Finding the rightmost set bit means finding the position of the bit which differs in both (so the bit in this position for one number will be 0 and for other as 1) . This rightmost bit is named as set_bit_no. so '&' with all numbers will separate out all number into two where these two numbers are separated out

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

still a little confused... can anyone give an example?

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

still a little confused... can anyone give an example?

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

Got it.

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

I got it.

for the same pair of number a, we have a^a = 0. Therefore, for N pairs, like a,b,c...a,b,c..., we still have a^b^c^...^a^b^c^...=0.

So if we have 2N+1 numbers with N pairs, the result of xor up all the number is the unrepeated one. If we have 2N+2 with N pairs and unrepeated number x y, the result = x^y and for sure result > 0. Then we split the numbers into two groups, to extract x and y respectively.

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

Awesomly awesome..!

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

I am going to try explain this more clearly.

First how xor works: if you have two numbers say 10 (1010) and 8 (1000), xor operation ( done using operator ^ ) will return the bits which are set in one number but not in the other.

10 -- 1010
08 -- 1000
---------------
^ -- 0010
---------------
Here the third bit is set(from right) in 10 but not in 8;

Now the problem states that there are 2n + 2 numbers such that n numbers are repeated.
eg. for n = 3: { 2, 3, 2, 4, 5, 4, 5, 6 }
there are two of ( 2, 4 & 5 ) but only one of ( 3 & 6 )

Lets take the first set of number that repeats..
when we xor a number with itself the result is zero.
2 ^ 2 = 0 (in binary: 10 ^ 10 )
4 ^ 4 = 0
5 ^ 5 = 0

Now we have actually got rid of the numbers that repeats twice.

so when we xor all the elements of the array : a[i] ^ a[i+1] ^ ... a[n], we would have performed, for the above example ( 2 ^ 3 ^ 2 ^ 4 ^ 5 ^ 4 ^ 5 ^ 6 )
Since the repeating elements are nullified :) we are left with ( 3 ^ 6 ), the xor of the two elements we need to find. If the elements are equal, the 2n+2 constraint is not valid and hence these two numbers would have also been nullified.

Now all we have to do is extract the two numbers (x, y) from ( 3 ^ 6 )

3 ^ 6 is
011
110
-----
101
-----
what we can infer from the above is that the last bit is set in one number and not in the other. ( and so does the first bit)

set_bit_no = xor & ~(xor-1);
This line in the above example by Rajat Rastogi can be replaced with

int set_bit_no = 1;
while ( ! ( xor & set_bit_no ) )
set_bit_no <<=1;

what it does is right shift, a bit at a time and check whether it is set in the result of ( 3 ^ 6 )
Why do we need this?

Because, when doing a xor, a bit is set only when it is set in one and not in the other. So we are actually isolating a trait of one number so that we can extract it from the result.

Now is the tricky part.

for(i = 0; i < n; i++)
  {
    if(arr[i] & set_bit_no)
     x = x ^ arr[i]; /*XOR of first set */
    else
     y = y ^ arr[i]; /*XOR of second set*/

}

Our example array: { 2, 3, 2, 4, 5, 4, 5, 6 }
x & y is initially zero;

Again we apply the same principle, but this time we check whether the rightmost bit in set in one each element of the array..if its set, it will be removed when a pair of the same item is encountered and thus we will be able to extract x;

Then we also xor the elems which does not have the right most bit set. So, we will be extracting all the pairs with this bit not set and y ( the pairs cancel each other and y remains )

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

I am going to try explain this more clearly.

First how xor works: if you have two numbers say 10 (1010) and 8 (1000), xor operation ( done using operator ^ ) will return the bits which are set in one number but not in the other.

10 -- 1010
08 -- 1000
---------------
^ -- 0010
---------------
Here the third bit is set(from right) in 10 but not in 8;

Now the problem states that there are 2n + 2 numbers such that n numbers are repeated.
eg. for n = 3: { 2, 3, 2, 4, 5, 4, 5, 6 }
there are two of ( 2, 4 & 5 ) but only one of ( 3 & 6 )

Lets take the first set of number that repeats..
when we xor a number with itself the result is zero.
2 ^ 2 = 0 (in binary: 10 ^ 10 )
4 ^ 4 = 0
5 ^ 5 = 0

Now we have actually got rid of the numbers that repeats twice.

so when we xor all the elements of the array : a[i] ^ a[i+1] ^ ... a[n], we would have performed, for the above example ( 2 ^ 3 ^ 2 ^ 4 ^ 5 ^ 4 ^ 5 ^ 6 )
Since the repeating elements are nullified :) we are left with ( 3 ^ 6 ), the xor of the two elements we need to find. If the elements are equal, the 2n+2 constraint is not valid and hence these two numbers would have also been nullified.

Now all we have to do is extract the two numbers (x, y) from ( 3 ^ 6 )

3 ^ 6 is
011
110
-----
101
-----
what we can infer from the above is that the last bit is set in one number and not in the other. ( and so does the first bit)

set_bit_no = xor & ~(xor-1);
This line in the above example by Rajat Rastogi can be replaced with

int set_bit_no = 1;
while ( ! ( xor & set_bit_no ) )
set_bit_no <<=1;

what it does is right shift, a bit at a time and check whether it is set in the result of ( 3 ^ 6 )
Why do we need this?

Because, when doing a xor, a bit is set only when it is set in one and not in the other. So we are actually isolating a trait of one number so that we can extract it from the result.

Now is the tricky part.

for(i = 0; i < n; i++)
  {
    if(arr[i] & set_bit_no)
     x = x ^ arr[i]; /*XOR of first set */
    else
     y = y ^ arr[i]; /*XOR of second set*/

}

Our example array: { 2, 3, 2, 4, 5, 4, 5, 6 }
x & y is initially zero;

Again we apply the same principle, but this time we check whether the rightmost bit in set in one each element of the array..if its set, it will be removed when a pair of the same item is encountered and thus we will be able to extract x;

Then we also xor the elems which does not have the right most bit set. So, we will be extracting all the pairs with this bit not set and y ( the pairs cancel each other and y remains )

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

I am going to try explain this more clearly.

First how xor works: if you have two numbers say 10 (1010) and 8 (1000), xor operation ( done using operator ^ ) will return the bits which are set in one number but not in the other.

10 -- 1010
08 -- 1000
---------------
^ -- 0010
---------------
Here the third bit is set(from right) in 10 but not in 8;

Now the problem states that there are 2n + 2 numbers such that n numbers are repeated.
eg. for n = 3: { 2, 3, 2, 4, 5, 4, 5, 6 }
there are two of ( 2, 4 & 5 ) but only one of ( 3 & 6 )

Lets take the first set of number that repeats..
when we xor a number with itself the result is zero.
2 ^ 2 = 0 (in binary: 10 ^ 10 )
4 ^ 4 = 0
5 ^ 5 = 0

Now we have actually got rid of the numbers that repeats twice.

so when we xor all the elements of the array : a[i] ^ a[i+1] ^ ... a[n], we would have performed, for the above example ( 2 ^ 3 ^ 2 ^ 4 ^ 5 ^ 4 ^ 5 ^ 6 )
Since the repeating elements are nullified :) we are left with ( 3 ^ 6 ), the xor of the two elements we need to find. If the elements are equal, the 2n+2 constraint is not valid and hence these two numbers would have also been nullified.

Now all we have to do is extract the two numbers (x, y) from ( 3 ^ 6 )

3 ^ 6 is
011
110
-----
101
-----
what we can infer from the above is that the last bit is set in one number and not in the other. ( and so does the first bit)

set_bit_no = xor & ~(xor-1);
This line in the above example by Rajat Rastogi can be replaced with

int set_bit_no = 1;
while ( ! ( xor & set_bit_no ) )
set_bit_no <<=1;

what it does is right shift, a bit at a time and check whether it is set in the result of ( 3 ^ 6 )
Why do we need this?

Because, when doing a xor, a bit is set only when it is set in one and not in the other. So we are actually isolating a trait of one number so that we can extract it from the result.

Now is the tricky part.

for(i = 0; i < n; i++)
  {
    if(arr[i] & set_bit_no)
     x = x ^ arr[i]; /*XOR of first set */
    else
     y = y ^ arr[i]; /*XOR of second set*/

}

Our example array: { 2, 3, 2, 4, 5, 4, 5, 6 }
x & y is initially zero;

Again we apply the same principle, but this time we check whether the rightmost bit in set in one each element of the array..if its set, it will be removed when a pair of the same item is encountered and thus we will be able to extract x;

Then we also xor the elems which does not have the right most bit set. So, we will be extracting all the pairs with this bit not set and y ( the pairs cancel each other and y remains )

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

I am going to try explain this more clearly.

First how xor works: if you have two numbers say 10 (1010) and 8 (1000), xor operation ( done using operator ^ ) will return the bits which are set in one number but not in the other.

10 -- 1010
08 -- 1000
---------------
^ -- 0010
---------------
Here the third bit is set(from right) in 10 but not in 8;

Now the problem states that there are 2n + 2 numbers such that n numbers are repeated.
eg. for n = 3: { 2, 3, 2, 4, 5, 4, 5, 6 }
there are two of ( 2, 4 & 5 ) but only one of ( 3 & 6 )

Lets take the first set of number that repeats..
when we xor a number with itself the result is zero.
2 ^ 2 = 0 (in binary: 10 ^ 10 )
4 ^ 4 = 0
5 ^ 5 = 0

Now we have actually got rid of the numbers that repeats twice.

so when we xor all the elements of the array : a[i] ^ a[i+1] ^ ... a[n], we would have performed, for the above example ( 2 ^ 3 ^ 2 ^ 4 ^ 5 ^ 4 ^ 5 ^ 6 )
Since the repeating elements are nullified :) we are left with ( 3 ^ 6 ), the xor of the two elements we need to find. If the elements are equal, the 2n+2 constraint is not valid and hence these two numbers would have also been nullified.

Now all we have to do is extract the two numbers (x, y) from ( 3 ^ 6 )

3 ^ 6 is
011
110
-----
101
-----
what we can infer from the above is that the last bit is set in one number and not in the other. ( and so does the first bit)

set_bit_no = xor & ~(xor-1);
This line in the above example by Rajat Rastogi can be replaced with

int set_bit_no = 1;
while ( ! ( xor & set_bit_no ) )
set_bit_no <<=1;

what it does is right shift, a bit at a time and check whether it is set in the result of ( 3 ^ 6 )
Why do we need this?

Because, when doing a xor, a bit is set only when it is set in one and not in the other. So we are actually isolating a trait of one number so that we can extract it from the result.

Now is the tricky part.

for(i = 0; i < n; i++)
  {
    if(arr[i] & set_bit_no)
     x = x ^ arr[i]; /*XOR of first set */
    else
     y = y ^ arr[i]; /*XOR of second set*/

}

Our example array: { 2, 3, 2, 4, 5, 4, 5, 6 }
x & y is initially zero;

Again we apply the same principle, but this time we check whether the rightmost bit in set in one each element of the array..if its set, it will be removed when a pair of the same item is encountered and thus we will be able to extract x;

Then we also xor the elems which does not have the right most bit set. So, we will be extracting all the pairs with this bit not set and y ( the pairs cancel each other and y remains )

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

can genius write a code for this.
I could not solve it when both non repeating numbers are either odd or even.
thanks in advance.

- miano June 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have idea to solve this but this contain extra space and my algo is limit to 0-9 number and this code can check to n no of time number not repeated

b[10]={0,0,0,0,0,0,0,0,0,0}//Storing initial value to it for(i=0;i<array.length;i++)
{
if(b[array[i]])
{
  b[array[i]]=b[array[i]]-array[i];//subtracting number if it is repeated
}
else
b[array[i]]=array[i];//storing value in array
 }
for(i=0;i<10;i++)
{
  if(b[i])
  printf("%d",b[i]);
}

- arpit2438735 August 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

If we can use extra memory, then using hashmaps we can do it. For each element we add hash(array[i])=1; and whenever we find containsKey(array[i])..we delete it so that at the end of scanning all the elements in array in hashmap we will be left with only one element.

If he asks for 2 or more, then all the remaining elements in the map are unique.

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

@shreyas we can't use extra space.although he gave me hint using xor into two groups but i could not understand his logic :(

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

We can solve 2n+2 array problem using the xor property. In first pass xor all the elements. now we have xor of two no.'s as output. now find the rightmost set bit of this output by using
mask = output & (output-1)
and this bit will be set in one of the no. so now use second pass and make two partitions of no. using this
mask
{if no. & mask == true
then a=a xor no.
else
b=b xor no.
}
by using this you can find both the no.

- codinggeek16 February 03, 2012 | 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