Amazon.com

  www.amazon.com
  www.amazon.com

Interview Question

Software Engineer Intern Interview(Student Candidate) Seattle, WA

what is your favourite data structure?

Answer

Interview Answer

1 Answer

0

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);
}

Amri on Feb 17, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.