Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
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).
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.
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);
}
}
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.
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)
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
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
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 ?
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;
}
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)?
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
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
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.
If space was not a constraint I would use a hashmap.
- Anonymous November 05, 2011Key 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.