I had an interview the other day with Amazon, and a question they asked me was pertaining to the following problem.
Given 2 integer arrays, containing any number of elem
One possible answer is similar to the HashMap
solution... IF you know that the integers are within a very small window. It would be similar to this: http://en.wikipedia.org/wiki/Bucket_sort
Basically, if the integers are guaranteed to be within a certain constant size window (i.e. all of them are 1-1000) then you can do it in constant space by incrementing each cell of index = whatever your number is. This is exactly the same as the HashMap
solution, except you don't need to be able to account for all possible integers like a HashMap
can, which lets you save on space. If this is unclear let me know in the comments and I'll explain further.