Sample questions
Question 1Easy
What is the time complexity of building a Huffman tree for n symbols with a min-heap?
- O(2^n)
- O(n)
- O(n^2)
- O(n log n)
Answer: D. O(n log n)
Explanation
There are n - 1 merges, each with O(log n) heap operations.
Step-by-step solution
- Build a min-heap of n symbols.
- Do n - 1 merges; each pops two and pushes one, costing O(log n).
- Total O(n log n).
Why the other options are wrong
- A. Huffman never tries all combinations.
- B. Linear time is possible only if frequencies are already sorted (two-queue method).
- C. That would be scanning for the two minimums each time without a heap.
Tip: n - 1 merges x O(log n) per heap operation.
Question 2Easy
To make 188 from denominations {1, 2, 5, 10, 20, 50, 100}, how many coins/notes does the greedy method use?
- 6
- 5
- 8
- 7
Answer: D. 7
Explanation
188 = 100 + 50 + 20 + 10 + 5 + 2 + 1, taking the largest denomination that fits each time: 7 items. For this canonical system greedy is optimal.
Step-by-step solution
- Take 100: 88 left.
- Take 50: 38 left. Take 20: 18 left.
- Take 10: 8 left. Take 5: 3 left.
- Take 2: 1 left. Take 1: 0 left.
- Seven coins/notes.
Why the other options are wrong
- A. Skips one denomination in the count.
- B. Counts only the notes 100, 50, 20, 10, 5.
- C. This uses three 1s for the last 3 instead of 2 and 1, which is 8 items.
Tip: Greedy is safe for standard currency denominations.
Question 3Easy
Which of these algorithms is greedy?
- Longest common subsequence DP
- Merge sort
- Kruskal's MST algorithm
- Floyd-Warshall
Answer: C. Kruskal's MST algorithm
Explanation
Kruskal always takes the cheapest edge that does not form a cycle.
Step-by-step solution
- Kruskal sorts edges by weight.
- It always adds the cheapest edge that does not form a cycle.
- That repeated locally best choice is the greedy pattern.
Why the other options are wrong
- A. LCS fills a DP table of subproblems.
- B. Merge sort is divide and conquer.
- D. Floyd-Warshall is dynamic programming over intermediate vertices.
Tip: Greedy classics: Kruskal, Prim, Dijkstra, Huffman, activity selection.
Question 4Easy
Children have greed factors [1, 2, 3, 4] and cookies have sizes [1, 1, 3]. A child is content if given one cookie with size >= its greed. What is the maximum number of content children?
- 2
- 3
- 4
- 1
Answer: A. 2
Explanation
Sort both and give each cookie to the least greedy remaining child it satisfies: cookie 1 -> child 1, cookie 3 -> child 2, so 2.
Step-by-step solution
- Sort greed [1, 2, 3, 4] and cookies [1, 1, 3].
- Cookie 1 satisfies child with greed 1.
- Next cookie 1 cannot satisfy greed 2; skip it.
- Cookie 3 satisfies child with greed 2.
- No cookies left: 2 children are content.
Why the other options are wrong
- B. Only one cookie is big enough for greed >= 2.
- C. There are only 3 cookies, so 4 is impossible.
- D. Wastes the chance to give the 1-cookie to the greed-1 child.
Tip: Sort both lists and use two pointers.
Question 5Medium
A greedy algorithm is guaranteed to be optimal when the problem has:
- The greedy-choice property and optimal substructure
- Negative weights
- Overlapping subproblems only
- A sorted input
Answer: A. The greedy-choice property and optimal substructure
Explanation
A locally best choice must be extendable to a globally optimal solution.
Step-by-step solution
- Greedy-choice property: some optimal solution starts with the locally best choice.
- Optimal substructure: after that choice, the rest is an optimal solution of the smaller problem.
- With both, repeating the greedy choice builds an optimal answer.
Why the other options are wrong
- B. Negative weights usually break greedy algorithms like Dijkstra.
- C. Overlapping subproblems point to dynamic programming, not greedy.
- D. Sorting is often a step, but it does not guarantee optimality.
Tip: Greedy = greedy-choice property + optimal substructure.
Question 6Medium
Trains arrive at 900, 940, 950, 1100, 1500, 1800 and depart at 910, 1200, 1120, 1130, 1900, 2000 (same order, 24-hour times). A train arriving at the same minute another leaves needs a separate platform. What is the minimum number of platforms needed?
- 2
- 4
- 3
- 6
Answer: C. 3
Explanation
Sort arrivals and departures and sweep, counting trains present; the peak is 3.
Step-by-step solution
- Sort all events by time; at equal times count the arrival first (a separate platform is needed).
- 900 arr (1), 910 dep (0), 940 arr (1), 950 arr (2), 1100 arr (3).
- 1120 dep (2), 1130 dep (1), 1200 dep (0), then 1500 and 1800 arrivals raise it only to 2.
- The peak count is 3.
Why the other options are wrong
- A. Misses that trains arriving at 940, 950 and 1100 are all present at 1100.
- B. Over-counts; no moment has four trains at once.
- D. That is one platform per train, which is never needed here.
Tip: Platforms = peak overlap; sweep sorted arrivals and departures.
Question 7Medium
Why does greedy (by value/weight ratio) fail for 0/1 knapsack?
- Ratios cannot be computed
- Items cannot be split, so a high-ratio item may waste capacity
- It is too slow
- It needs negative weights
Answer: B. Items cannot be split, so a high-ratio item may waste capacity
Explanation
With capacity 50 and items (60,10), (100,20), (120,30), greedy by ratio gets 160, but 100 + 120 = 220 is optimal.
Step-by-step solution
- Example: capacity 50, items (60,10), (100,20), (120,30).
- Ratios are 6, 5, 4, so greedy takes the first two: value 160, weight 30.
- The third item (weight 30) no longer fits and cannot be split.
- Taking items 2 and 3 gives 220, which is better.
Why the other options are wrong
- A. Ratios are easy to compute: value divided by weight.
- C. Greedy is fast; the problem is a wrong answer, not speed.
- D. Knapsack weights are positive; negatives play no role here.
Tip: Fractional knapsack: greedy. 0/1 knapsack: DP.
Question 8Medium
To minimise the maximum pair sum when splitting [3, 5, 2, 3, 6, 4] into 3 pairs, the greedy strategy pairs:
- adjacent elements after sorting, giving 11
- the two largest together, giving 11
- smallest with largest after sorting, giving a maximum pair sum of 8
- elements in the given order, giving 10
Answer: C. smallest with largest after sorting, giving a maximum pair sum of 8
Explanation
Sorted [2, 3, 3, 4, 5, 6] paired outside-in gives (2,6), (3,5), (3,4) with sums 8, 8, 7.
Step-by-step solution
- Sort: [2, 3, 3, 4, 5, 6].
- Pair smallest with largest: (2,6), (3,5), (3,4).
- Pair sums 8, 8, 7: maximum 8.
Why the other options are wrong
- A. Adjacent pairs give (5,6) = 11, not optimal.
- B. Putting the two largest together makes the maximum larger.
- D. Given-order pairs give (3,5), (2,3), (6,4) with max 10, and it is not a strategy.
Tip: Minimise the max pair sum: sort and pair ends inward.
Question 9Hard
Characters have frequencies A:10, B:15, C:12, D:3, E:4, F:13, G:1. What is the total number of bits in the Huffman encoding of the whole message (sum of frequency * code length)?
- 174
- 136
- 160
- 146
Answer: D. 146
Explanation
The total encoded length equals the sum of all merge costs when the two smallest weights are repeatedly combined: 146. A fixed-length code would need 174.
Step-by-step solution
- Sorted: 1, 3, 4, 10, 12, 13, 15.
- Merges:
1+3=4,
4+4=8,
8+10=18,
12+13=25,
15+18=33,
25+33=58.
- Total = 4 + 8 + 18 + 25 + 33 + 58 = 146.
Why the other options are wrong
- A. 174 is 58 * 3, fixed-length coding.
- B. 136 is an arithmetic slip.
- C. 160 is not the sum of merge costs.
Tip: Total Huffman bits = sum of all merged weights.
Question 10Hard
Split "ababcbacadefegdehijhklij" into as many parts as possible so that each letter appears in only one part. What are the part sizes?
- 9, 7, 8
- 9, 8, 7
- 9, 15
- 8, 9, 7
Answer: A. 9, 7, 8
Explanation
Extend each part to the last occurrence of every letter it contains; close it when the scan reaches that end: 9, 7, 8.
Step-by-step solution
- Record the last index of each letter.
- Scan, extending the current part's end to the last index of every letter seen.
- 'ababcbaca' ends at index 8: size 9.
- 'defegde' size 7, then 'hijhklij' size 8.
Why the other options are wrong
- B. Right sizes, wrong order.
- C. Merges the second and third parts unnecessarily.
- D. Cuts the first part too early; 'a' appears at index 8.
Tip: Precompute last[c]; cut when i == max(last of letters seen).
Practise all 39 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →