Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A pair of ropes is connected as a chain: lengths 3, 4, 5 are joined in the order listed, each join costs the sum of the two lengths being joined, and the joined rope then acts as one piece. What is the total cost of joining 3 and 4 first, then the result with 5?

  1. A. 12
  2. B. 21
  3. C. 24
  4. D. 19

Correct answer: D. 19

Join 3 + 4 = 7 (cost 7), then 7 + 5 = 12 (cost 12): total 19. Counting only the last join gives 12, joining 4 and 5 first gives 9 + 12 = 21, and 24 adds the total length twice.

More Coding Interview Problems questions

Exams that ask this topic