Amazon Interview Question for SDE1s


Country: Luxembourg
Interview Type: Phone Interview




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

Take XOR of all the numbers of array. Resulting number in XOR is the answer.

- Nitin Gupta February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can do this in O(nlgn) by sorting the array and searching

To do it in O(n) I think you need O(n) extra space. You can use a hash map and then hash the numbers as you see. If the number is not present in the HashMap, you add it. If it is already present you remove it. In the end the only number remaining in the hash map will be the number you need.

- KK February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

XOR all numbers.

- Gourab Roy February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

XOR all the numbers and get the result in O(n) with no extra space

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

As the others pointed out, just use XOR. Example in Java

private static int findOddRepXor(int[] a) {	
	    int val = 0;
	    for (int i = 0; i < a.length; i++){
	        val = val ^ a[i];
	    }
	    return val; 
	}

- svarvel February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The Logic Is if Number Is repated Even Number Of Times Then By Doing Xor It Becomes 0.But The Odd Number Times Element By Doing Xor It gives ItS value.
#include<stdio.h>
int arr[]={5,4,3,3,4,1,1};
main()
{
int i,count=0;
for(i=0;i<=6;i++)
{
count^=arr[i];
}
printf(" %d",count);
}

- Uda Anil Kumar March 07, 2014 | 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