Given two arrays of integers, return a new array containing only the common elements.
Anonymous
Be sure to ask for more details about this one: Are the given arrays sorted? Does the order of the elements in the output array matter? Should the output array have duplicate entries? These all will drastically affect your answer. In the case that the answer to all of those questions is "no," then a decent algorithm would involve placing all of the elements of the first array in a hash table, then looking up the elements from the second in that table. As usual, you'll be asked about the complexity of your algorithm. The one described is O(n). Also, whenever you mention a hash table, they'll tend to ask about performance degrading (when many values hash to the same bucket, making a long linked list in that bucket). In the case of this problem, you can probably "throw out" all duplicate values, making this less of a problem. Again, review your data structures and algorithms.
Check out your Company Bowl for anonymous work chats.