Amazon Interview Question

what is your favourite data structure?

Interview Answer

Anonymous

Feb 18, 2013

For the question: Find all pairs that sum up to a given no x: 1. Sort the array using quick sort O(nlogn) 2. See code, call with allPairs (a, 0, a.length - 1); {{{ void allPairs (int a[], int l, int r, int x) { if(a[l] + a[r] == x) print("%d %d", a[l], a[r]); else if (a[l] + a[r] > x) allPairs(a, l, r-1, x); else allPairs(a, l+1, r, x); }