Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

If space was not a constraint I would use a hashmap.

Key is the integer and value is the number of occurrences.

As you iterate through each element of the array hash it into the hashmap. If key already exists then simply update the number of occurrences.

Once completed iterate along the keys in the hashmap and return key with the even number of occurrences.

- Anonymous November 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We could simply sort the numbers O(n*log(n)) and do a scan+count on the fly. Stop when we hit the count is even and found a non-matching number.

I guess, the intent is to get algorithm with time complexity O(n) and space complexity O(constant).

- Laxmi Narsimha Rao Oruganti November 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

best solution on the page

- xyz November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Find all the unique elements in the array in the first scan , now XOR all elements in the array with all these unique elements, the result of XOR is the answer.

- puneet November 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

import java.util.Hashtable;
import java.util.Set;
import java.util.Iterator;


public class FindOddNumber {

public static void main(String args[]){

int arr[]={1,2,4,3,4,3,3};

Hashtable table=new Hashtable();
int result=0;
for (int i=0;i<arr.length;i++){
result=result^arr[i];
if(table.containsKey(arr[i]))
continue;
else
table.put(arr[i],true);
}

Set set = table.keySet();
Iterator it = (Iterator) set.iterator();

int a;
while(it.hasNext()){
a=((Integer)it.next()).intValue();
result=result^a;
}

for(int i=0;i<arr.length;i++)
result=result^arr[i];

System.out.println(result);
}
}

- puneet November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the for loop towards the end is not der ...plz remove it.... my mistake.....we hav already XORed all elements in the array above , no need to do it again.

- puneet November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong solution , it is correct for only ur input

consider this
{1,2,4,3,4,3,3,5}

unique elements are 1,2,3,4,5
answer should be 4
but with ur solution it is 1 (the XOR of 1,2,3,4,5 is 1)

- getjar.com/todotasklist my android app November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i am gettin correct solution ...... juss remove d last for loop in my code

- puneet November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If it helps, here is why the code works.

If we append all unique elements to the collection, then the collection would have all numbers repeated even number of times except one. In this situation, XOR of all values would yield the only element that occurred odd number of times.

Here the code is first getting XOR of all array and then the XOR values is continued (see result is not reset to zero) to the unique value set.

Hence, the code works.

Thanks,
Laxmi

- Laxmi Narsimha Rao Oruganti November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

As a pseudocode:

1. create a HashTable and all elements to it // This collection will have jus one copy all elements in the input array
2. also XOR all elements in the array into a variable "result".
// result will have the XOR of numbers repeated odd number of times.
3. Now, while iterating the collection, XOR those elements with the "result"
4. At the end of this XORing Loop, we will have "result" having the even no. of elements

- aragorn November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good approach Puneet and nice explanation Laxmi

- Rigel November 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very Smart Puneet .. Basically you are just reversing the question to all even and one odd. But wont the computation time/space increase with finding unique elements in array ?

- Aniket November 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good approach.. Somewhat simplified implementation.

public static int getEvenRepeatedNum(int a[]){
		int result=0,i;
		
		Hashtable<Integer,Integer> table = new Hashtable<Integer, Integer>();
		
		for (i=0;i<a.length;++i){
			result^=a[i];
			table.put(a[i], 1);
		}
		Enumeration<Integer> e = table.keys();
		 
	    //iterate through Hashtable keys 
	    while(e.hasMoreElements())
	      result^=e.nextElement();
	  
		return result; 
	}

- Amol November 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int a[] = {3, 4, 5, 4, 5};
int result = a[0];
for (int i = 1; i < 5; ++i)
{
result ^= a[i];
}
cout << result << endl;

return 0;

- Albeo February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanx for the explanation Laxmi....i realise now i shud hav explained in words too .... but dis is d first time im posting something on careercup , so .....

- puneet November 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Above all solutions are either going to take O(n) solution with O(n) space or O(n^2) solution.

And there is nothing tricky in such solution.
Can we do it in less that O(n^2)?

- nitin November 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sort and count nlogn and no space complexity

- cant_think_ November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

although the solution posted by puneet (nicely explained by olnrao) is correct.
but getting unique elements requires extra space if using hashing or O(nlogn) if sort and then find unique elemnts.

solution with O(N) time complexity with constant space seems not possible any thoughts

- getjar.com/todotasklist my android app November 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Take a hashmap, and a variable called OddNumber;
2. start reading from the first number.
3. Add that number to hashmap. if hashmap return true add that number to OddNumber, if it return false, subtract is from OddNumber.
4. After you read all number, what you will have in OddNumber is your answer.

ex: 3 8 6 5 3 6 4 2 6 8 4 5 4

- Rama December 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The problem seems to fresh. If the integers are all positive, we can have the following solution:

unsigned int find_odd(vector<int>& v){
      for(unsigned int i=0;i<v.size();i++){
		if(v[abs(v[i])]>0)
			v[abs(v[i])]=-v[abs(v[i])];
      }
       for(unsigned int i=0;i<v.size();i++){
           if(v[i]<0) return i;
       }

O(n) time, O(1) space

Test: 2,2,1,1,3,3,3
first round the array becomes: 2,2,-1,1,3,3,3
second round output 3

- geliang2008 December 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

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

let myBox=empty;
let result=0;
for all elements{
if myBox.notContains(element)
add to myBox;
else
result^=element;
}
print result;

- gautam March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Cant we simply do like this :

1. keep two pointers p1, p2.
2. P1 keeps the count of one unique element
3. p2 keeps the count of the other unique element
4. after traversing the array return the element associated with which ever count p1 or p2 is even.

- Anonymous November 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ Anonymous

at least check your approach with 1 or 2 examples before posting here.

Totally absurd and wrong.

- getjar.com/todotasklist my android app November 07, 2011 | 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