Amazon Interview Question






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

Since the question doesn't talk about any sorting, i think, [keeping the data along with a counter in a structure] linked list would be a good solution.

- Niti December 05, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

method A: hashtable. detect collision. complexity O(n)

method B: sorting, then compare adjacent elements. detect elements which are equal. complexity O(nlogn).

- kenny December 13, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I had a similar question from Expedia:

How would you find the position of the duplicate element(1 duplicate at most) w/ O(1) space. This means you can use static memory but no dynamic allocation like Hash Tables. I didn't solve this problem but mentioned bit manipulation using XOR. Anyone have a clever idea on this?

- Jack January 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you be more elaborate on O(1) space requirement?

- raki December 28, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the elements in the list are consecutive like 1,2,3,4,1,5 . then it can be easily solved by XORing. 1^2^3^4^1^5=1.
If they are not consecutive then sort the values and compare its adjacent values using == or XOR.
nothing more than is feasible.

- sanjithkanagavel July 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think, if you don't want to use dynamic memory, then first sort the array. Then compare two adjacent elements, using xoring function. Means when two elements are equal, then xor result will be zero otherwise 1.

- Swati January 26, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why would you need to XOR it? You might as well just compare the 2 numbers using an '==' operator.

- Anonymous June 18, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

aXORa == 0.
So aXORbXORa = b. XOR is commutative.

So XOR all elements of the array in turn till it becomes zero will give u the repeated number. But i think this can fail sometimes. nybody has a better solution?

- shawshank January 26, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

assuming 4 bit integer mapping

1010 = 10
will cancel out with
1000 and 0010 = 8 and 2

therefore an array like
[10, 2, 8 , 3] would give 3 as the result, which is not true.

- fjay February 06, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you sort the array, you lose the position of the integer, so sorting isn't allowed.

- Jack January 26, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution spontaneously came to me...

1. Calculate the sum from 1 to N, using the series formula.
2. Since there is at most 1 duplicate, you can find the position by searching in either direction, depending on which position you want to find.

The running time is then O(n) and O(1) storage(only original array).

- Jack February 02, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution works only when numbers are consecutive 1...n,if the array contains something like 14 13 5 6 7 7 8 1 then sorting would give you 1 5 6 7 7 8 13 14 and as you traverse the array and reach the element 7 & 7 you could either use the XOR 7^7 and check or use == operator.Correct me if I have misunderstood your approach.

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

this solution only works if there is additional constraint

size of array = n
all numbers lie with 1- n-1 range.

Am i missing something

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

Yes. The array is a set of 1 to N.

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

In Perl:

my %h;
$h{$_}++ for @A;

@dups;
for(keys %h){
push @dups, $_ if $h{$_}>1
}
return @dups;

- Brandon April 03, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use counting sort. It takes O(n)

- Moyo August 28, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

From the above example

2,3,1,4,5,6,7,7 the sum is 35
but the sum is 35 even for 2,3,1,4,5,6,6,8.. and the repeated
number is 6!
How does just getting the sum help in finding the repeated number?

So, I think Hash which takes O(1) and extra space is the best solution
Second best is to sort and scan the sorted array O(nlogn)

- mp January 13, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 votes

mp, I think the assumed constraint here (in Vish's solution) is that all integers in the range of 1 to N are present in the array and only one integer is repeated. Your example is missing 7.

- skybound January 13, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Jack's Expedia question sol:

first pass - xor everything to get duplicate number.
second pass - iterate over array to find position of duplicate number.

so each pass is O(n) and it did not use any extra space.

- abc June 16, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

XOR would work only if ALL the elements in the array are repeated and there is just one number which appears once. XOR-ing would help us find that sole number.

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

A kind of bit vector like solution, in O(n).
Traverse the array, setting corresponding bit to 1. If the bit already one, then we found our duplicate, if not just keep traversing. Worst case would be if the last element is the repeated one.

But a CATCH is wasted space in creating the bitvector of that size. If range is known, then its great else, we have to make some guesses or give the range of Int.MAX_Value as the size of bit vector

- prolificcoder August 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Bit vector sounds expensive since a bit vector supports sequential access, it takes O(n) time to scan.

Mergesort & comparing adjacent duplicates will be O(nlogn). This is expensive as well. O(nlogn) is expensive than O(n). But, this method doesn't need any extra space.

Hash table is the fastest with O(1) retrieval & access times. But a hash table takes extra O(n) space.

So, we need to decide if we want space(mergesort & comparing adjacent duplicates) or time(the lovely hash table).

- Helper December 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Convert int array to an Integer array. Add contents of the new array to a TreeSet. when the .add method returns false we have encountered the duplicate.

- arch April 03, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

A simple solution is in O(1).
The sum of first n natural numbers i.e., 1+2+3+...+n if the numbers are in sequence is given by: n*(n+1)/2. So u can apply the above formula i.e., For example:
The below is the given sequence:
2,3,1,4,5,6,7,7.
Now as there are 8 numbers:
8*(8+1)/2 = 36.
Now the sum of the above elements is:35.
So u can easily say that 7 is repeated as sum is 1 less than it should be.

- Vish October 19, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

and how it is O(1)? You have to calculate the sum of all numbers which is O(n), no?

- q June 01, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just to add upon q's statement:
-------------------------------

from Jack's statement N is known.

1) so sum of numbers = N(N+1)/2.... which does not take much time. This can be O(1).

2) Now finding the duplicate element is direct. If N is 5 and 5 is repeated, new sum will be 20. So 20 - 15 (original sum) = 5.

3) Traverse the array and find the first position of '5'.
This is O(n).

- raki December 28, 2008 | 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