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?
- A. 33
- B. 29
- C. 26
- 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
- Array [1, 2, 4, 1, 3, 4]. How many contiguous subarrays have a bitwise OR of exactly 7?
- Set A = {1, 2, 4, 7} and set B = {3, 5, 6, 8}. Choose two different elements from A and two different elements from B. How many choices…
- Half-open intervals [0,2), [1,3), [2,4), [3,5), [4,6). What is the minimum number of intervals to remove so the remaining intervals are…
- Array a = [2, 3, 5, 1, 4, 5, 2, 3]. How many contiguous subarrays have an even length and a sum divisible by 5?
- In the binary string "1101001100", find the length of the longest substring that has equally many 0s and 1s and whose length is a multiple…
- Values [1, 9, 4, 12, 7, 15, 3]. How many unordered pairs of distinct positions have an absolute difference of at least 5?