Domanda di colloquio di Amazon

Find if two values in an array add to a third given value.

Risposta di colloquio

Anonimo

8 lug 2015

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.