Data Structures & AlgorithmsHardInfosys SP/DSE · Product Company SDE

Problem: A problem requires finding all pairs in an array that sum to a target, allowing duplicate pairs to be counted once. Which is the best average-case approach and its complexity?

  1. A. A hash map counting value frequencies, achieving O(n) average time
  2. B. Only possible in O(n log n) via sorting, no faster approach exists
  3. C. Comparing every pair directly without any sorting or hashing, O(n^2), and this is optimal
  4. D. Sorting plus nested loops, O(n^2)

Correct answer: A. A hash map counting value frequencies, achieving O(n) average time

Counting how many times each value appears in a hash map, then for each distinct value checking whether its complement exists (handling the self-paired case separately), finds all valid pairs in a single O(n) average-case pass.

More Coding Interview Problems questions

Exams that ask this topic