Amazon Interview Question for Software Engineer / Developers






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

I feel hashing is the simplest and the most efficient solution. What do you guyz think?

- Anonymous July 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sum of n no - n(n+1)/2
sum of integer in an array - sum
Duplicate no is n - ((n(n+1)/2) - sum)

e.g 1,1,2,3,4
n is 5
sum of 5 no - 5(5+1)/2 = 15
sum of no 11
Duplicate no - 5 - (15 - 11)

- Anonymous June 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is not given that the integers are 1,2,3,...,n. Is it?

- LOLer June 28, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

its given that there is an array of integers.

- raj July 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use hashing

- mastee July 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@mastee
jaake baap ko bol

- Anonymous May 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the first will not be applicable in : N = 0,1,2,3,4,5,5
Here the Sum of n no : 20
Sum of integer : 28
Subtract: 8

Here 8 does not have any resemblance here.

- raj July 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Partition the array in odd an even numbers. If there are more odd numbers than even, partition the odd numbers by the next bit and repeat. If there are more even numbers than odd, look up the duplicate in the even numbers partition.

It is similar to the selection algorithm because on average the numbers left after each iteration is about half, so the complexity is approx:

O(N/2 + N/4 + N/8 + ...) which is O(N)

C++ example for N == 8:

#include <algorithm> // for std::swap
#include <iostream>
using namespace std;

int partition(int* a, int N, int mask)
{
    int m = -1;
    for (int i = 0; i != N; ++i)
    {
        if (a[i] & mask)
        {
            swap(a[++m], a[i]);
        }
    }
    return m + 1;
}

template<int N>
int findDuplicate(int (&a)[N])
{
    int lower = 0, upper = N, mask = 1;
    for (; mask < N; mask <<= 1)
    {
        int i = partition(a + lower, upper, mask);
        if (i > (upper - lower) / 2)
        {
            upper = i;
        }
        else
        {
            lower = i;
        }
    }
    return a[lower];
}

int main()
{
    int a[] = { 2, 7, 6, 4, 5, 6, 3, 1 };
    cout << findDuplicate<8>(a) << endl;
    return 0;
}

- cristi.vlasceanu July 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

keep it short and simple.

- Erik July 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What are you doing?

- Adrian August 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes hashing is efficient solution with extra storage it can be done in O(n).
Another method would be to sort the numbers in increasing order and then check if a duplicate exists. Complexity: O(nlgn)

- Tom July 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort the array and apply binary search

- Anu August 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

seriously? Binary search for which number?

- prem March 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

yup, hashing's the way to go if space is not an issue.By the way sorting doesn't work if the array is read only.

- RIP September 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

other than hashing sorting technique is d best way !!! and while sorting we can find the duplicate element ....so no need to travel the array onced more !!!

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

iterate through the array and add the number with the length. then iterate once again and the one greater than 2*n is the answer.

for( i =0 ; i <length; i++)
{
   if(a[i] > n)
   {
      return a[i]; // duplicate number
   }
   a[i]+=n;
}

- fragzilla March 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@fragzilla it is not said all number lies between 1 to n it can be >n
ex 1 1000 1 then your answer will be 1000 .

- ridercoder October 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

XOR all the integers in the array, the result will the number which is a duplicate

- prem March 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Abe chutiye kyun apna aur humara dono ka time waste kar raha hai.

- Anonymous July 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(int i=0;i<size;i++)

if(array[Math.abs(array[i])] > 0)	
            array[Math.abs(array[i])] = -array[Math.abs(array[i])];
      else
   	    System.out.print(Math.abs(array[i]) + " ");

- VJ March 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(int i=0;i<size;i++)
   if(array[Math.abs(array[i])] > 0)	
      array[Math.abs(array[i])] = -array[Math.abs(array[i])];
   else //this is a duplicate
      return Math.abs(array[i]);

- VJ March 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(int i=0;i<size;i++)
   if(array[Math.abs(array[i])] > 0)	
      array[Math.abs(array[i])] = -array[Math.abs(array[i])];
   else //this is a duplicate
      return Math.abs(array[i]);

- VJ March 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(int i=0;i<size;i++)
   if(array[Math.abs(array[i])] > 0)	
      array[Math.abs(array[i])] = -array[Math.abs(array[i])];
   else //this is a duplicate
      return Math.abs(array[i]);

- VJ March 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One of the best approach using time O(n) and space O(n) can be done using hashing, we will keep inserting the array elements in the hash table and check that if the element is present in the hash table from before then it is a repeating element in the array and the element will be the final output

Implementation:

int findelement(int arr[], int n){
	unordered_set<int> s;
	for(int i = 0; i < n; i++){
		if(s.find(arr[i]) != s.end())
			return arr[i];
		s.insert(arr[i]);
	}
}

- swapnilkant11 July 22, 2019 | 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