B
Bill Butler
Greg Young said:As you mention that this is an interview question ...
My guess is that they were looking to see how you approached the problem (and what questions you
asked like "is the data sorted", "What's the general usage of the method i.e. 3 items vs 300")
All of those items have alot to do with how you would want to implement the routine.
I would *hope* that this was the interviewer's intent.
Asking "what's the most efficient implementation?" is a loaded question.
Is the data sorted?
Is it OK to sort the data, or do we need to copy the Array and sort that?
Is the array so large that copying it causes memory issues?
Is the data set small enough that O(N), O(NlogN) etc not come into play?
What does the actual data normally look like? (let me Splain)
If the data has very few gaps in it a brute force approach might be fastest.
Example: Data = all integers from 1-100 Sum = 101
One pass through the data will suffice to find a valid result.
OK, this is an extreme example, but if the data has few gaps, you will tend to get multiple valid
solutions to the problem. In the example above there are 100 pairs that add up to 101 (assuming
(x,y) is different from (y,x))
Thus a brute force search can succeed, possibly faster that the sort
Anyway....food for thought
Bill