Data Structures & AlgorithmsHardTCS NQT · Infosys
Problem: Huffman coding is applied to symbol frequencies 5, 9, 12, 13, 16, 45, always merging the two smallest weights. What is the total cost, meaning the sum of all merged weights?
- A. 214
- B. 229
- C. 224
- D. 245
Correct answer: C. 224
Merges are 5+9=14, 12+13=25, 14+16=30, 25+30=55, 45+55=100 with total 14+25+30+55+100 = 224. Merging the wrong pair first changes the sum, which is the trap.
More Coding Interview Problems questions
- Problem: Find the maximum of every window of size 3 in the array below, printing the list of window maxima. python from collections import…
- Problem: Compute the Z-array (z[i] = length of the longest common prefix of s and s[i:], with z[0] set to 0) for s = 'aabxaab'. python def…
- Problem: Count the palindromic substrings of 'abacaba' (every occurrence counts, so single characters count too). python def count(s)…
- Problem: A directed graph with V vertices is relaxed with Bellman-Ford. After V - 1 full rounds of relaxing all edges, one more round…
- Problem: A Fenwick tree of size 8 receives point adds (1,+5), (3,+2), (5,+7), (8,+1) with the usual add and prefix-query code, then prints…
- Problem: You want the maximum number of non-overlapping intervals. Which greedy rule is provably optimal?