View on GitHub

DSA-Helper

Here’s a comprehensive list of Bit Manipulation Techniques and Interview Practice Problems with related LeetCode/GeeksforGeeks questions where possible.


1. Basics of Bit Manipulation

Understanding the core bitwise operators is essential for solving bit manipulation problems.

Example Questions:

  1. LeetCode #191 - Number of 1 Bits
  2. LeetCode #231 - Power of Two
  3. LeetCode #371 - Sum of Two Integers
  4. GeeksforGeeks - Count set bits
  5. LeetCode #136 - Single Number
  6. GeeksforGeeks - Check whether K-th bit is set or not
  7. LeetCode #268 - Missing Number
  8. GeeksforGeeks - Check if a number is odd or even
  9. LeetCode #190 - Reverse Bits
  10. GeeksforGeeks - Toggle K-th bit

2. Power of Two Checks

Key Insight: Powers of two have only one bit set in their binary representation.

Example Questions:

  1. LeetCode #231 - Power of Two
  2. GeeksforGeeks - Check if a number is a power of two
  3. LeetCode #342 - Power of Four
  4. LeetCode #326 - Power of Three
  5. LeetCode #693 - Binary Number with Alternating Bits
  6. LeetCode #137 - Single Number II
  7. GeeksforGeeks - Count total set bits in all numbers from 1 to N
  8. LeetCode #338 - Counting Bits
  9. GeeksforGeeks - Turn off the rightmost set bit
  10. GeeksforGeeks - Position of rightmost set bit

3. Counting Set Bits

Key Insight: Each x &= (x - 1) removes the lowest set bit, making it ideal for counting bits.

Example Questions:

  1. LeetCode #191 - Number of 1 Bits
  2. GeeksforGeeks - Brian Kernighan’s Algorithm
  3. LeetCode #338 - Counting Bits
  4. GeeksforGeeks - Flip all bits of a number
  5. LeetCode #476 - Number Complement
  6. GeeksforGeeks - Swap all odd and even bits
  7. LeetCode #89 - Gray Code
  8. GeeksforGeeks - Find parity of a number
  9. LeetCode #67 - Add Binary
  10. LeetCode #405 - Convert a Number to Hexadecimal

4. Swapping Numbers Using XOR

Key Insight: XORing a number with itself results in 0, so swapping can be done in-place without using additional space.

Example Questions:

  1. LeetCode #136 - Single Number
  2. GeeksforGeeks - Swap two numbers without a temporary variable
  3. LeetCode #137 - Single Number II
  4. GeeksforGeeks - Swap all odd and even bits
  5. LeetCode #260 - Single Number III
  6. LeetCode #645 - Set Mismatch
  7. GeeksforGeeks - Find the only odd occurring element
  8. LeetCode #260 - Single Number III
  9. LeetCode #137 - Single Number II
  10. LeetCode #268 - Missing Number

5. Reversing Bits

Key Insight: By shifting bits one at a time and using bitwise OR, you can reverse the order of bits in a number.

Example Questions:

  1. LeetCode #190 - Reverse Bits
  2. GeeksforGeeks - Reverse bits of a number
  3. LeetCode #405 - Convert a Number to Hexadecimal
  4. LeetCode #190 - Reverse Bits
  5. GeeksforGeeks - Find whether a number is a palindrome in binary representation
  6. LeetCode #1290 - Convert Binary Number in a Linked List to Integer
  7. GeeksforGeeks - Reverse individual bits of a number
  8. LeetCode #405 - Convert a Number to Hexadecimal
  9. GeeksforGeeks - Reverse a linked list using bitwise operators
  10. GeeksforGeeks - Convert Binary to Gray code

This list provides a comprehensive guide to mastering bit manipulation, with more than 10 examples in each section for targeted practice on LeetCode and GeeksforGeeks.