Amazon Interview Question
Software Engineer / DevelopersFor third, can we go for...
Compare first 3 numbers, see which is occuring 3 or 2 times.
If one number is occuring 2 times, other number is the one.
Else if all 3 numbers are same, scan through array to find a number different than the current one.
Worst time complexity is O(n) and average case is O(n/2) and best case is O(1).
1)
here is how it can be done
the idea is, at every new name read, pick either current winner or new name as winner, with probabilities in proportion to (n-1/n):1/n
- akshay kumar June 03, 2008this will ensure that all the names read till count = n will have equal chance (1/count) of getting picked as winner.
2) i would go for HashSet.
3) XOR would be fastest.