Find if two values in an array add to a third given value.
Anonymous
I provided an O(n^2) solution, but could not find an O(n) solution until the interviewer provided the hint that it was a "searching problem." Although he continued to explain the solution, I had the "aha" moment that subtracting the given value from any element in the array provided the value to be found in the array. O(n) can be achieved by iterating over the array, subtracting the value from the given, and checking the presence of that difference in a hash table of each array value.
Check out your Company Bowl for anonymous work chats.