LearnFrenzy
Sign in Take a free test

DSA & Algorithms · Topic 12 of 12

Greedy and Bit Manipulation: questions with answers

Greedy choice and when it fails, activity selection, fractional knapsack, Huffman coding, intervals and scheduling; bitwise operators, masks, XOR tricks, counting bits and two's complement.

Questions
45
Timed sets
3
Answers run or computed
37
Your accuracy
–

What this topic covers

18 subtopics
  • Counting bits5
  • Greedy foundations5
  • XOR tricks5
  • Bit masks4
  • Interval scheduling4
  • Two's complement4
  • Bit tricks3
  • Bitwise operators3
  • Reachability2
  • Scheduling2
  • Activity selection1
  • Bit manipulation1
Show all 18 subtopics
  • Coin change1
  • Custom ordering1
  • Fractional knapsack1
  • Huffman coding1
  • Matching1
  • Two-pass greedy1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

A greedy algorithm makes the locally best choice at each step and never reconsiders. It is fast and simple, but correct only when the problem has the greedy-choice property – which is why many questions here pair the greedy answer with a brute-force check. Bit manipulation works directly on binary representations: AND to test or clear bits, OR to set them, XOR to toggle or cancel, shifts to multiply or divide by powers of two. Numbers are drawn in binary where it helps.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Activity selection Beginner Practice Answer computed

Activities (start, finish): (1, 4), (3, 5), (0, 6), (5, 7), (3, 9), (5, 9), (6, 10), (8, 11), (8, 12), (2, 14), (12, 16). One room; an activity may start when the previous one finishes. What is the maximum number of activities?

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 4

Explanation

Always pick the compatible activity that finishes earliest: (1, 4), (5, 7), (8, 11), (12, 16). Finishing early leaves the most room for the rest – an exchange argument proves it optimal.

Question 2 Huffman coding Intermediate Practice Answer computed

Characters a–f have frequencies 5, 9, 12, 13, 16, 45. Huffman coding repeatedly merges the two least frequent nodes. How many bits does the encoded text take in total?

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 224

Explanation

Merges: 5 + 9 = 14, 12 + 13 = 25, 14 + 16 = 30, 25 + 30 = 55, 45 + 55 = 100. The total cost is the sum of the merged weights: 14 + 25 + 30 + 55 + 100 = 224 (f gets a 1-bit code, a and b 4-bit codes).

Question 3 Greedy foundations Advanced Practice One or more answers

For which problems is a natural greedy algorithm guaranteed optimal? Select all that apply.

Coin change with arbitrary denominations (largest first)
Huffman coding (merge two least frequent)
Fractional knapsack (best value per weight)
0/1 knapsack (best value per weight)
Activity selection (earliest finish first)
Show answer and explanation
Correct answer BCE

Explanation

The first three have greedy-choice proofs. 0/1 knapsack and general coin change need dynamic programming – the counterexamples earlier in this topic show greedy failing.

Why the other options are wrong

  • AFails for coins {1, 3, 4} and amount 6.
  • DFails when leftover capacity is wasted (value 160 vs 220).
42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DSA & Algorithms

All 12 topics

Topics, levels and full mocks
  1. 01 Time and Space Complexity45 questions · 3 levels
  2. 02 Arrays and Strings45 questions · 3 levels
  3. 03 Linked Lists45 questions · 3 levels
  4. 04 Stacks and Queues45 questions · 3 levels
  5. 05 Hashing45 questions · 3 levels
  6. 06 Recursion and Backtracking45 questions · 3 levels
  7. 07 Sorting and Searching45 questions · 3 levels
  8. 08 Trees and BST45 questions · 3 levels
  9. 09 Heaps and Priority Queues45 questions · 3 levels
  10. 10 Graphs45 questions · 3 levels
  11. 11 Dynamic Programming45 questions · 3 levels
  12. 12 Greedy and Bit Manipulation45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links