Data Structures & AlgorithmsHardProduct-company MCQ pattern
Characters a, b, c, d, e occur 4, 7, 9, 12 and 20 times. With an optimal Huffman code, how many bits are needed to encode the whole text?
- A. 120
- B. 115
- C. 116
- D. 156
Correct answer: B. 115
Merges: 4 + 7 = 11; 9 + 11 = 20; 12 + 20 = 32; 20 + 32 = 52. One optimal set of code lengths is a 4, b 4, c 3, d 2, e 1 (the tie between the two 20s can give another set with the same total), so 4·4 + 7·4 + 9·3 + 12·2 + 20·1 = 115 (also the sum of the merged weights, 11 + 20 + 32 + 52). A fixed 3-bit code would need 52 × 3 = 156.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Bellman–Ford from A relaxes the directed edges in the fixed order C→D (2), B→C (3), A→B (4), A→C (9), B→D (7). After the FIRST full pass…
- A directed graph has edges 1→2, 2→3, 3→1, 3→4, 4→5, 5→6, 6→4, 6→7, 7→8. How many strongly connected components does it have?
- An undirected graph has edges 1–2, 2–3, 3–1, 3–4, 4–5, 5–6, 6–4, 6–7. How many articulation points (vertices whose removal disconnects the…
- What is the time complexity of g(n)? python def g(n): if n <= 1: return for i in range(n): pass # O(1) g(n // 2) g(n // 2) g(n // 2)
- How many of these statements about DFS are correct? (i) In DFS of an undirected graph, every non-tree edge is a back edge (there are no…
- Keys are inserted into an empty BST in the order 45, 15, 79, 90, 10, 55, 12, 20, 50. What is the postorder traversal? python def insert(t…