Amazon Interview Question
Software Engineer / DevelopersCan someone please explain why this is working? This is an awesome solution but for some reason I don't understand why it is working and under what conditions it keeps working...
Ex: Any number XORed with the same number will give 0. 0 XORed with 0 will also give 0. Allrepeated numbers XOR will result in 0. Finally the non-repeated-number XOR 0 will be the number itself.
Access time for hash table O(1), insert time O(1)
2 solutions for above question ( one with using hash map, 1 without using hashmap)
1. Without using HashMap
- Sort the list O(nlgn)
- Keep a counter c = 0, last digit = -1
Start scanning the list
if(lastDigit!= arr[i])
{ if(c==1)
{
print lastDigit;
lastDigit = arr[i];
c=1;
}
}
else
c++;
- Check separately for last number of array
Algorithm 2 : With HashMap
1. Insert all elements in hash map with value = 0
2. start scanning the array, search elements and increment count
hashmap[arr[i]]++;
3. start the scan again
if hashmap[arr[i]] == 1
print it.
It is very basic pseudo code, not in any specific language, customize it in the language you want.
One solution that came into my mind was to prepare a trie. If you get a string, insert into the trie. If you get the string again, delete the node from trie. At the end, we will left with only one string, which is the answer.
another is to get int[] for each string. and then simply xor. It does not include extra memory as well
Item* Reverse(Item* head)
{
if (!head || !head->next)
return head;
Item* last = head;
Item* newHead = head->next;
last->next = NULL;
while (newHead)
{
Item* temp = newHead;
newHead = newHead->next;
temp->next = last;
last = temp;
}
newHead = last;
return newHead;
}
Item* Reverse2(Item* head)
{
if (!head || !head->next)
return head;
Item* temp = head->next;
Item* newHead = Reverse2(head->next);
temp->next = head;
head->next = NULL;
return newHead;
}
If there are numbers from 1 to 99 in the array...solution can be... Traverse the entire array...when a number is reached suppose n set, suppose array[n] is set to k, then set it to -k (i.e. it's negative value). When it is reached again, reset it to it's positive value. Again traverse array from 1 to 99 and see which index is still negative...that is the answer! Time O(n) and no extra space.
Here when you do XOR of any number it does XOR(^) in the bits.
So finally when entire array is XORed,it will have bits left of the number which is not repeated and all other bits will be zeroed out by XOR.
For e.g if you have array [9] = {1,2,3,4,5,3,2,1,4}
Here array[0] = 1.
1 ^ 2 = 3
3 ^ 3 = 0
0 ^ 4 = 4
4 ^ 5 = 1
1 ^ 3 = 2
2 ^ 2 = 0
0 ^ 1 = 1
1 ^ 4 = 5
So, Final result is 5.
Okay, here's what the function for xor strings should be like:
But, it's behaving weird, I'm gonna take a break, in the meantime..if somebody can spot, the bug, well, bingo then !
char * xorStrings(char *str1, char *str2){
int i=0, n;
int lenCommon;
char *strShort, *strLong, *strXor;
strShort = (strlen(str1) > strlen(str2))?str2:str1;
strLong = (strlen(str1) > strlen(str2))?str1:str2;
i = 0;
lenCommon = strlen(strShort);
n = strlen(strLong);
strXor = (char*)malloc((size_t)sizeof(char) * (n+1));
n -= lenCommon;
while(lenCommon-- ){
strXor[i] = strLong[i] ^ strShort[i];
i++;
}
while(n--){
strXor[i] = strLong[i] ^ 0;
i++;
}
strXor[i] = '\0';
return strXor;
}
there is an easier solution for question 2:
- SunDavid.S September 21, 2008int result = arr[0];
for(int i = 1;i < 199;i ++)result = result XOR arr[i];
return result;
when integer -> string, just extending the operator XOR would be OK.