Greedy Technique · Algorithms
11 questions · asked in 7 of 27 years (2000–2026)
MCQ 7 · MSQ 1 · NAT 31-mark 6 · 2-mark 5
2000Questions per year2026
Questions 1–5 of 11
Define to be the maximum amount earned by cutting a rod of length n meters into one or more pieces of integer length and selling them. For , let denote the selling price of a rod whose length is meters. Consider the array of prices:
which of the following statements is/are correct about ?
which of the following statements is/are correct about ?
One or more options are correct.
Answer: (A), (C)
Let be the best value for length . Then .
, , , , , .
.
Checking all ways to cut 7 shows exactly three reach 18:
- one piece of 7 (18)
- 6 + 1 (17 + 1)
- 3 + 2 + 2 (8 + 5 + 5)
Every other split gives at most 17 (for example 4 + 3 gives 17, and 3 + 3 + 1 gives 17).
(A) is true: . (B) is false, since 19 is never reached. (C) is true: there are three different solutions. (D) is false, since 3 + 2 + 2 uses three pieces and gives 18.
Answer: (A), (C)
, , , , , .
.
Checking all ways to cut 7 shows exactly three reach 18:
- one piece of 7 (18)
- 6 + 1 (17 + 1)
- 3 + 2 + 2 (8 + 5 + 5)
Every other split gives at most 17 (for example 4 + 3 gives 17, and 3 + 3 + 1 gives 17).
(A) is true: . (B) is false, since 19 is never reached. (C) is true: there are three different solutions. (D) is false, since 3 + 2 + 2 uses three pieces and gives 18.
Answer: (A), (C)
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?
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?
Answer: (B)
Frequencies: a 1, b 2, c 2, d 2, e 3 (total 10).
Huffman: join a(1) with a letter of frequency 2. To keep the tie rule (b, c, d in dictionary order), join a with d 3. Then b + c 4. Then ad + e 6. Then 4 + 6.
Code lengths: a 3, d 3, b 2, c 2, e 2 (b c d holds).
Length = 13 + 22 + 22 + 23 + 32 = 3 + 4 + 4 + 6 + 6 = 23
Answer: 23
Huffman: join a(1) with a letter of frequency 2. To keep the tie rule (b, c, d in dictionary order), join a with d 3. Then b + c 4. Then ad + e 6. Then 4 + 6.
Code lengths: a 3, d 3, b 2, c 2, e 2 (b c d holds).
Length = 13 + 22 + 22 + 23 + 32 = 3 + 4 + 4 + 6 + 6 = 23
Answer: 23
Consider the weights and values of items listed below. Note that there is only one unit of each item.
The task is to pick a subset of these items such that their total weight is no more than 11 Kgs and their total value is maximized. Moreover, no item may be split. The total value of items picked by an optimal algorithm is denoted by . A greedy algorithm sorts the items by their value-to-weight ratios in descending order and packs them greedily, starting from the first item in the ordered list. The total value of items picked by the greedy algorithm is denoted by .
The value of - is ____________.
The task is to pick a subset of these items such that their total weight is no more than 11 Kgs and their total value is maximized. Moreover, no item may be split. The total value of items picked by an optimal algorithm is denoted by . A greedy algorithm sorts the items by their value-to-weight ratios in descending order and packs them greedily, starting from the first item in the ordered list. The total value of items picked by the greedy algorithm is denoted by .
The value of - is ____________.
Answer: 16
Ratios of value to weight: item 4 = 12, item 1 = 6, item 3 = 5, item 2 = 4.
- Greedy: pick item 4 (2 kg, value 24). Item 1 (10 kg) can no longer fit. Pick item 3 (4 kg, value 20). Item 2 does not fit .
- Optimal: item 1 alone gives 60, which is better than items 2 and 4 together (52) or items 2 and 3 together (48) .
Difference = 60 44 = 16
Answer: 16
- Greedy: pick item 4 (2 kg, value 24). Item 1 (10 kg) can no longer fit. Pick item 3 (4 kg, value 20). Item 2 does not fit .
- Optimal: item 1 alone gives 60, which is better than items 2 and 4 together (52) or items 2 and 3 together (48) .
Difference = 60 44 = 16
Answer: 16
A message is made up entirely of characters from the set X= {P,Q,R,S,T}. The table of probabilities for each of the characters is shown below:
If a message of 100 characters over X is encoded using Huffman coding, then the expected length of the encoded message in bits is_____
If a message of 100 characters over X is encoded using Huffman coding, then the expected length of the encoded message in bits is_____
Answer: 225
Huffman steps: T (0.08) and R (0.17) merge to 0.25; S (0.19) and P (0.22) merge to 0.41; 0.25 and Q (0.34) merge to 0.59; then 0.41 and 0.59 merge.
Code lengths: Q 2, S 2, P 2, T 3, R 3.
Average length = 0.342 + 0.192 + 0.222 + 0.083 + 0.173 = 2.25 bits. For 100 characters, this gives 225 bits.
Answer: 225
Code lengths: Q 2, S 2, P 2, T 3, R 3.
Average length = 0.342 + 0.192 + 0.222 + 0.083 + 0.173 = 2.25 bits. For 100 characters, this gives 225 bits.
Answer: 225
Suppose P, Q, R, S, T are sorted sequences having lengths 20, 24, 30, 35, 50 respectively. They are to be merged into a single sequence by merging together two sequences at a time. The number of comparisons that will be needed in the worst case by the optimal algorithm for doing this is ____.
Answer: 358
In the worst case, merging two lists of sizes m and n takes m + n 1 comparisons. The best way is to always merge the two smallest lists, as in Huffman coding.
- 20 + 24 = 44 43
- 30 + 35 = 65 64
- 44 + 50 = 94 93
- 65 + 94 = 159 158
Total = 43 + 64 + 93 + 158 = 358
Answer: 358
- 20 + 24 = 44 43
- 30 + 35 = 65 64
- 44 + 50 = 94 93
- 65 + 94 = 159 158
Total = 43 + 64 + 93 + 158 = 358
Answer: 358
Keep practising