Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Five numbers 1, 2, 3, 4, 5 are inserted in that order into an initially empty min-heap (array form, sift-up after each insertion). How many swaps are performed in total?

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

Correct answer: A. 0

Inserting an increasing sequence into a min-heap never triggers a swap, because each new value is larger than its parent. The count is zero, the trap being to assume each insertion swaps at least once.

More Coding Interview Problems questions

Exams that ask this topic