Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: In a binary max-heap stored in an array with 1-based indexing, the array is [40, 30, 35, 10, 20, 25, 28, 5, 8, 12]. An operation sets the value at position 10 to 45 and then sifts it up. Which position (1-based) does the value 45 end at?

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

Correct answer: C. 1

Sift-up swaps the value with its parent while it is larger, climbing the implicit tree (parent of i is i//2). Forgetting the parent index convention gives a wrong final position.

More Coding Interview Problems questions

Exams that ask this topic