Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

This could be solved with hash map where keys are elements in an array and values are number of occurrences.

Scan the array linearly, if a key already exists in a hash map, (assuming there is only one duplicate element), return the key.

if its possible that there are duplicates of multiple elements, then create an array which contains duplicate elements. Whenever key.exists is true, add that element to the array.

In the end , when you have iterated all the elements, the array will contain all the duplicate elements.

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

Counting Sort O(n)

Traverse once to find dups O(n)

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

Better approach would be to just sum up the array elements and subtract n(n+1)/2 from it. This approach ofcourse assumes the there is only one duplicate.

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

Your solution does not work.
Your solution work if all the elements are present from 1 to n except. and finding this missing one.

- Tanvi Reddy February 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That can also work in that case also.

but it will fail in case of large numbers where n*(n+1)/2 will go out of bounds

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

@tanvi can you illustrate when this does not work ?

@nitin You are right. however, you have to choose a bigger datatype than what we are summing together. I was just outlining the algo.

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

@pavan: This wont work for this array 1, 2, 3, 2, 5 . The question doesn't invalidate my input. It is still in the range 1 - 5

- any February 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The best answer is use XOR

time complexity : O(n) - 1 iteration
space complexity : O(1)


int xor=0;
for (int i=0;i<array.length-1;i++){
xor=xor ^ i ^ arr[i]
}

The final value is duplicate value.

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

@ analog76: your solution will not work for the case 1 2 3 2 5
according to your code it would be (2^4)=6 not 2.
and if last element is missing then ans would be 0.
To find out 2 and 4 separately we need to divide set (0,1,2,3,4,1,2,3,2) in two parts according to least active bit of ans(here 6)(110).
so we can separate them 2 groups having 2'nd bit from right 0 and 1. groups are {0,1,1,4}and {2,3,2,3,2}
then take XOR of both groups to find out 4 and 2 separately.

- shubham February 22, 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