Data Structures & AlgorithmsHardFlipkart/Uber OA · placement

Sort [−4, 3, −3, −5, −4, 5, 2] and, for each i, move two pointers to find the triplet sum closest to -2. What is that closest sum?

  1. A. 0
  2. B. -3
  3. C. -2
  4. D. -1

Correct answer: C. -2

Sorting gives [−5, −4, −4, −3, 2, 3, 5]. Fix i, then if the current sum is below the target move lo right, else move hi left, tracking the smallest |sum − -2|. The closest achievable sum is -2.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic