Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA

Ropes of lengths 4, 3, 2 and 6 are joined two at a time; joining ropes of lengths x and y costs x + y and creates one rope of length x + y. What is the minimum total cost to join all ropes into one?

  1. A. 33
  2. B. 29
  3. C. 26
  4. D. 15

Correct answer: B. 29

Repeatedly join the two shortest ropes, which is the Huffman-style greedy with a min-heap. Joining the longest ropes first builds a more expensive chain.

More Coding Interview Problems questions

Exams that ask this topic