Interview Question

Anonymous Interview

Given an integer N and an array of unsorted integers A find

  all pairs of numbers within A which add up to N. (This is the only question I can remember and I had trouble thinking of an answer at the time)
Tags:
technical, algorithm
Answer

Interview Answer

3 Answers

0

Something something hash map... basically you can hash all the remainders of N-A[x] and if you later come across one of those remainders you just add the tuple to your return set.

Interview Candidate on Aug 1, 2013
0

Interview candidate's answer is better, but here is a brute force implementation (which is not as efficient).

void printNumPairs(int* A, int sizeA, int val)
{
 for(int outer = 0; outer < sizeA; outer++)
 {
  for(int inner = outer+1; inner < sizeA; inner++)
  {
   if(val == A[outer]+A[inner])
   {
    cout << A[outer] << " + " << A[inner] << " = " << val << endl;
   }
  }
 }
}

JB on Oct 3, 2013
0

Dynamic programming

Anonymous on Dec 3, 2013

Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up