Amazon Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Yea, I believe the output of the Huffman encoding should be a string of 0's & 1's or L's & R's to indicate the path from the root to the respective letters, such that the length is minimal with respect to the given string, the constructed frequency table, and, of course, the Huffman tree, as depicted.
I guess the problem statement is to create Huffman code for the string "AAAAAABBCCDDEEFFFFF"
- SR February 03, 2013Solution: <Well known method, just adding for the completeness sake>
1. Find the list of all the symbols and their respective frequencies.
A 6, B 2, C 2, D 2, E2 , F 5.
2. Aim is to create a binary tree so that A, B, C, D, E, F are leaf nodes. Code for any leaf node would be path from the root in terms of Left of Right turn you take on every node from the root. Lets denote 0 for left and 1 for right. Hence for the example root -> lc->rc->lc->leafnode, code for leaf node is 010.
Now Huffman code dictates that the symbol with the maximum frequency should have smallest code. Hence the binary tree we would like to create should be such that A with maximum frequency should have min length of code.
3. How do we do it?
a. Store all the leaf nodes in a min heap.
b. Delete two nodes from the heap.
c. Create a binary tree from the two nodes such that rootNode.freq = leafNode1.freq + leafNode2.freq;
d. Add that rootNode to the min heap.
<Continue this process till we have only one element and the resultant tree would be something similar to what is being asked.>