Question
Consider the string abbccddeee. Each letter in the string must be assigned a binary code satisfying the following properties:
-
For any two letters, the code assigned to one letter must not be a prefix of the code assigned to the other letter.
-
For any two letters of the same frequency, the letter which occurs earlier in the dictionary order is assigned a code whose length is at most the length of the code assigned to the other letter.
Among the set of all binary code assignments which satisfy the above two properties, what is the minimum length of the encoded string?
Official answer and solution
- Official answer
- B
- From
- Official GATE 2021 answer key
Solution
Analyze character frequencies, apply the lexicographic constraint for equal-frequency letters, and construct an optimal prefix tree under the given rules.
The string is abbccddeee, with character counts:
Total length of the string is characters.
Constraint 1 requires a prefix code (valid binary tree leaf assignment). Constraint 2 requires that for letters of equal frequency, earlier dictionary letters must have codeword length later letters. Specifically, for the equal-frequency letters (all frequency 2):
Consider a standard Huffman tree without constraint 2: grouping , then , then , then . This assigns length 2 to and length 3 to . However, this gives , violating constraint 2 because occurs before in dictionary order.
To satisfy , consider binary tree structures with 5 leaves:
Using codeword lengths (which satisfies Kraft's inequality: ):
- Assign length 2 to the highest frequency letter (frequency 3).
- Among , to satisfy constraint 2, assign length 2 to and , and length 3 to :
- The remaining leaf of length 3 is assigned to (frequency 1).
Calculate the total encoded length:
This satisfies both conditions and achieves the minimum possible length, matching option B.
B
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2021 Computer Science and Information Technology - Session 2 (CS2)
- Question
- 36 of 65, printed as CS Q.26
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.