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?
- A. 31
- B. 29
- C. 38
- 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
- Problem: How many integers n with 1 ≤ n ≤ 100 have a binary representation that reads the same forwards and backwards?
- Problem: What is the sum of the smallest prime factors of the integers 2 through 30 (each integer contributes its smallest prime divisor)?
- Problem: How many positive divisors does the number 2^10 × 3^4 have?
- Problem: What are the last two decimal digits of 3^200?
- Problem: What is the maximum value of a XOR b over all pairs (a, b) taken from the array [3, 10, 5, 25, 2, 8] (a and b may be equal)?
- Problem: The minimum number of characters to delete from "(()))(" to make the parentheses balanced (every ( has a matching ) after it) is…