Bit Manipulation Problems for Coding Interviews
Bit manipulation uses the binary form of numbers for compact state and fast tricks: XOR to find the odd one out, masks for subsets, shifts for powers of two and counting set bits. Many problems that look like maths reduce to a bitwise identity.
Know the operators and their precedence, how negative numbers are stored and how to test, set and clear a bit. Practise single number, counting bits, power of two and subset enumeration with a bitmask.
Coding problems
- Number of One Bits (Basic)
- Counting Bits (Intermediate)
- Hamming Distance (Basic)
- Reverse Integer (Intermediate)
- Decode XORed Array (Basic)
- Bitwise AND of a Range (Intermediate)
- Count Equal XOR Triplets (Advanced)
- Longest XOR Subarray (Intermediate)
- Count Subsets With Given XOR (Intermediate)
- Counting Bits (Basic)
- Maximum XOR Pair Under Limit (Advanced)
- Minimum Operations on Binary String (Basic)
- IP Longest Prefix Match (Advanced)
- Exact Cover Solver (Advanced)
- Boolean Parenthesization Count (Basic)
- Binary Linked List to Integer (Basic)
- Maximum Width of Binary Tree (Intermediate)
- Sum of Root-to-Leaf Binary Numbers (Basic)
- Integers Without Consecutive Ones (Advanced)
- String to Integer (Intermediate)
- Evaluate Reverse Polish Notation (Intermediate)
- Basic Calculator (Advanced)
- Basic Calculator II (Intermediate)
- Super Ugly Number (Intermediate)
- Sort Integers by Number of 1 Bits (Basic)
- Gray Code (Intermediate)
- Total Hamming Distance (Intermediate)
- Complement of Base 10 Integer (Basic)
- Divide Two Integers (Intermediate)
- Design Bitset (Intermediate)
- UTF-8 Validator (Intermediate)
- Elias Gamma Encoding (Basic)
- Varint Encoding (Basic)
- Minimum AND Cost Walk (Advanced)
- Minimum XOR Sum of Two Arrays (Advanced)
- Chalkboard XOR Game (Intermediate)
- Parse Lisp Expression (Advanced)
- Basic Calculator III (Advanced)
- Longest Nice Subarray (Intermediate)
- Shortest Subarray With OR at Least K (Intermediate)
- XOR Queries of a Subarray (Intermediate)
- Minimum Suffix Flips (Basic)
- Score After Flipping Matrix (Intermediate)
- Reverse Bits (Basic)
- Sum of Two Integers Without Plus (Intermediate)
- Bitwise ORs of Subarrays (Intermediate)
- Minimum Flips to Make a OR b Equal to c (Intermediate)
- Binary Number With Alternating Bits (Basic)
- Prime Number of Set Bits (Basic)
- Concatenation of Consecutive Binary Numbers (Intermediate)
- Maximum XOR After Operations (Intermediate)
- XOR Sum of All Pairs Bitwise AND (Advanced)
- Minimize XOR (Intermediate)
- XOR Operation in an Array (Basic)
- Decode XORed Permutation (Advanced)
- Original Array From Prefix XOR (Basic)
- Neighboring Bitwise XOR (Basic)
- Bitwise XOR of All Pairings (Intermediate)
- Largest Combination With Positive AND (Intermediate)
- Smallest Number With Exactly N Divisors (Intermediate)
Related
- Sliding window problems
- Dynamic programming problems
- Graph problems
- Practice coding problems online
- All interview question topics
Frequently asked questions
How do I get good at bit manipulation problems?
Solve a few problems of each difficulty, name the pattern after you solve each one, and revisit the ones you needed hints for a week later. The list on this page is ordered so you can start with the basic ones.
Can I run bit manipulation problems online here?
Yes. Every problem opens in an editor that runs your code against test cases in Java, Python, C++, Go, JavaScript and six more languages, with no setup.