Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Ropes of lengths 4, 3, 2 and 6 are joined pairwise; joining two ropes costs the sum of their lengths, and the next join uses the two shortest current ropes. What is the minimum total cost?

  1. A. 31
  2. B. 29
  3. C. 38
  4. D. 14

Correct answer: B. 29

With a min-heap: 2 + 3 = 5, 4 + 5 = 9, 6 + 9 = 15, total 29. Joining in the given order gives 7 + 9 + 15 = 31, joining the largest first gives 10 + 13 + 15 = 38, and forgetting the final join gives 14.

More Coding Interview Problems questions

Exams that ask this topic