## Amazon Interview Question for Software Engineer / Developers

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

My answer I gave was to put all the numbers in a hash table where the key where the numbers and the values where the number of times the number was seen. Then just go through all the numbers and see which ones are not divisible by 2. This was the best answer I could come up with on the spot, and I wasn't able to answer the last part of how it could be done without another data structure.

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

XOR all of 'em

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
0

You are a genius! That was so simple why didn't I think of that when they asked me?! Thanks for the simple solution, I'll keep bit-wise operations in mind next time :p

Comment hidden because of low score. Click to expand.
0

You are a genius! That was so simple why didn't I think of that when they asked me?! Thanks for the simple solution, I'll keep bit-wise operations in mind next time :p

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
0

Genius? People, I hope you realize that this is a classic answer that most of smart people know the answer to. Thnx.

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

result = 0;
for each element
{
result ^= element[i];
}
return result;

Comment hidden because of low score. Click to expand.
0

good job

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

sum all the numbers. subtract one element at a time, if result is even the number just subtracted is answer. the xor solution is best imo.

Comment hidden because of low score. Click to expand.
0

Cool.

Comment hidden because of low score. Click to expand.
0

I think this should not work..

Consider.. 2,2,3,3,4,4,5

Sum = 23

So according to your algo answer should be 3 but this is not the case.

Please correct me if I am wrong...

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

Are u sure for XOR answer ??

public class Xor {

public static void main(String[] args) {
int arr[] = { 1,1,1,2,2,3,4,4,4,7,7,5};
int result = 0;
for (int i = 0; i < arr.length; i++) {
result = result ^ arr[i];
//System.out.println( (i+1) + ": " + result);
}

System.out.println("Final Result : " + result);

}
}

The result that was given is 3 instead of 2 .

Comment hidden because of low score. Click to expand.
0

XOR is best solution. You have taken wrong input. only one number occurs odd time and rest all numbers occurs in even times..

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

@Hi : Always read the question carefully.
XORing is the best answer for such even-odd occurrence questions.

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

Just FYI to any potential Amazon interviewees -- this question or a variation of it is VERY common, and I've seen it every time I was interviewed. Knowing the XOR answer will put you ahead of a lot of people.

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

Hi ,

I just thought of another way of doing it.

product all the numbers.
divide the product by each of the numbers. Find the square root of the division. If its whole. then the integer is odd one. as even number of integer multiplication should have whole square root.

but again no zeroes here. but seriously,, how the hell did u come up XOR man??!!

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

Q: An integer array where there are repeating numbers and all but one repeat an odd number of times ex [ 2,2,2,4,4,7,7,7,3,5] 4 repeats even number of times, [2,1,7,3,3] 3 repeats even number of times . Find it

A: Do an XOR of the corresponding bits of integers in the array, and do a NOT on the result

Comment hidden because of low score. Click to expand.
0

this won't work.. try (2,3,5,5) your solution doesn't give 5.

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

a good test case for this solution is, {0,1,1,2,2} ;-)

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

Please read the question carefully it is not that straight forwards "All but one is repeated even number of times" and we have to find any one which is repeated odd number of times. So, here the XOR wont work.

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.

### 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.