Bit Manipulation Interview Questions: Patterns and Strategies
Master Bit Manipulation problems for coding interviews — common patterns, difficulty breakdown, which companies ask them, and study tips.
Bit manipulation questions test your ability to think at the most fundamental level of computing. While less common than array or string problems, they are a hallmark of interviews at top tech companies, precisely because they reveal a candidate's comfort with binary logic, efficiency, and low-level optimization. Mastering a few core patterns can turn these seemingly cryptic puzzles into straightforward exercises.
Common Patterns
Understanding these patterns will help you decode most bit manipulation problems.
1. Checking, Setting, Clearing, and Toggling Bits
This is the foundational skill. You must be fluent in using masks to manipulate individual bits.
- Check if the i-th bit is set: Use AND with a mask
(1 << i). - Set the i-th bit: Use OR with a mask
(1 << i). - Clear the i-th bit: Use AND with a mask of the complement
~(1 << i). - Toggle the i-th bit: Use XOR with a mask
(1 << i).
def manipulate_bits(num, i):
# Check
is_set = (num & (1 << i)) != 0
# Set
num_set = num | (1 << i)
# Clear
num_cleared = num & ~(1 << i)
# Toggle
num_toggled = num ^ (1 << i)
return is_set, num_set, num_cleared, num_toggled
2. XOR Pattern
XOR is incredibly powerful. Key properties: a ^ a = 0, a ^ 0 = a, and XOR is commutative/associative. This pattern solves problems like finding the single unique number in an array where all others appear twice, or swapping values without a temporary variable.
def find_unique(nums):
# Finds the number that appears once (others appear twice)
unique = 0
for num in nums:
unique ^= num
return unique
def swap(a, b):
a = a ^ b
b = a ^ b # b now equals original a
a = a ^ b # a now equals original b
return a, b
3. Brian Kernighan's Algorithm
This is an efficient trick to count the number of set bits (1s) in a number. The key insight: n & (n - 1) flips the least significant set bit to 0. The number of operations equals the number of set bits.
def count_set_bits(n):
count = 0
while n:
n &= (n - 1) # Drop the lowest set bit
count += 1
return count
Difficulty Breakdown
Our dataset of 218 questions breaks down as follows: Easy (46, 21%), Medium (95, 44%), Hard (77, 35%). This distribution is telling. The significant portion of Medium and Hard problems indicates that interviewers use bit manipulation not for trivial checks, but for complex problem-solving. Easy questions often test basic operations. Medium problems combine patterns (e.g., using XOR and bit masking together). Hard questions typically involve dynamic programming with bitmasks (like Bitmask DP for problems such as "Maximum Product of Word Lengths" or "Minimum Cost to Connect Two Groups of Points"), requiring you to represent states compactly.
Which Companies Ask Bit Manipulation
Bit manipulation is a favorite screening tool at companies known for deep technical interviews. The top askers are:
If you are targeting these companies, dedicating time to this topic is non-negotiable.
Study Tips
- Memorize the Core Operations. Have the check, set, clear, and toggle operations for a single bit committed to memory. They are your building blocks.
- Internalize XOR Properties. The XOR pattern appears repeatedly. Be ready to recognize when a problem involves pairing or canceling out duplicate values.
- Practice Bitmask DP Separately. For Hard problems, treat "Bitmask DP" as its own sub-topic. Focus on learning how to use an integer to represent a visited set or a subset selection.
- Draw It Out. When stuck, write the binary representation of your numbers. Visualizing the bits makes operations like
n & (n-1)intuitive.
Bit manipulation is a concise way to test fundamental CS understanding. By focusing on these patterns and their applications, you can approach these questions with confidence.