Amazon Interview Question for Software Engineer / Developers






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

xor the entire array elements..the result is the req ans.

- sd April 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sd

Very nice soln!

- andy April 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sd
awesome soln

- kidil June 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

very nice

- Ravi October 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In (c++ like) pseudocode.

hash_map<int,int> num2count;
  for (int i=0 ; i< array_size; ++i) {
    iterator itr - num2count.find(array[i])
    if (itr !=num2count.end())
        num2count.erase(itr); //This number occurred twice
    else 
        num2count[array[i]]=1;
  }
  return num2count.begin()->first;

- Jasmeet April 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If B[] is the given array, create an array Arr[] of size 10 which holds zeros (so the array indexes will be 0,1,2..9).

for (i=0;i<len(B);i++)
{
  Arr[B[i]] += 1;
}

Now B[i] should have 2's for all the duplicate elements and 1 for the lone one. Can use hashtable too.

- Answer April 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nah.. sorry, just realized why this won't work. But we can still use hashtable.

- Answer April 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

xor the entire array elements..is the best one

- swapnesh April 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope, how about if a number has 3 duplicates?

- Ming April 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if 3 duplicates or more and space is a constraint(no hastable and extra arrays) , then sort the elements may be quick sort and then have two pointers and keep incrementing the second ptr when repeats and then update first=second.

curr
next=current+1
while(1){
{
if curr!=next
return current
break

while(curr==next)
next=next+1

curr=next
next=curent+1
continue;
}

- Anonymous April 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

does this work for the input 1 1 2 2 2 3

- GP November 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What you mean by XOR entire array element ? If you are going to be taking element one by one. THen it would be O(N^2)

- Bala April 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Even I am not sure what do you mean by XORing the entire array, do you mean XOR each element with every other? Because if A[] is an array then A^A does not make sense to me because A is simply a pointer

- Vinay April 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void dupl(int * arr,int n){
int xor=arr[0];
for(i = 1; i < n; i++)
xor ^= arr[i];
cout<<xor;//The result
}

- avatar April 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what are u trying to achieve here?

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

public class FindUnique{

public static void main(String[] args){
Integer[] ar = new Integer[]{1,8,5,8,1,3,5};
List<Integer> list = Arrays.asList(ar);
for(Integer i: list){
int frequency = Collections.frequency(list, i);
if(frequency == 1){
System.out.println(i + " occurs only once");
break;
}
}
}
}

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

MS and standard of ms question ?

- xyz August 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

XORing?? for one, I cant think of a way to implement it.. and two.. xoring every element woth the remaining wall give a O(n^2) time complexity..

I guess Hash map will give the best time complexity..O(n).. although there may be a better method to improve on space complexity..

- NotThatBad March 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey guys, the XORing works as O(n).. say the list is 1,2,3,4,1,2,3. Suppose we start with k=0 and xor every term with k in sequence (n terms).. we remain with 4. This is because Xoring twice with the same number gives back the original number. Hence at the end we are left with the term that is not repeated. Heres the program.

#include<stdio.h>
#include<conio.h>

void main()
{
 int list[7]={1,2,3,4,1,3,2};
 int k=0,i;

 for(i=0;i<7;i++)
 {
   k=k^list[i];
 }
 printf("Number with no duplicate: %d",k);
 getch();
}

//OUTPUT: Number with no duplicate: 4

- NotThatBad March 25, 2011 | 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