Amazon Interview Question
Software Engineer InternsCountry: United States
Interview Type: Phone Interview
For those who r not getting the question.i will explain.nyway nice question karan
ok now for those who r not getting the question,this question is about picking one element from each array of total n array and add up these number to make it zero.hope it is clear.
will post solution soon.
I have a solution but that is not proper (as we make n^n different set ,among these set we have to find one set whose sum is zero) but this way is not proper.
Assuming the arrays are sorted (or sorting them is allowed as a preprocessing step), we can solve this by using binary search. I haven't tried the algorithm, but I am guessing this approach works:
- maximus September 08, 20141. Sort the arrays. Let's call them array A, B and C respectively.
2. Iterate the array that has the least number of elements. Let's assume array A has the least number of elements.
3. For every element A[i], find an element B[j]using binary search such that A[i] + B[j] is close to zero. Call this sumX.
4. Using sumX, find an element C[k] using binary search such that sumX + C[k] is zero. Call this sum finalX.
5. If finalX is found, print {a[i], b[j], c[k]} and return true (since the question is to find if such a set exists).
6. If finalX is not found, repeat steps 3-6 until end of array A is reached.
7. Return false if finalX is not found and end of array is reached.
Have I missed something? Let me know if this solution passes all test cases.