KatInterview

693. Binary Number with Alternating Bits

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method 1: Iterative Approach

This method involves a straightforward check of the bits one by one.

Solution

Binary Number with Alternating Bits in Python

class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
        # Python integers handle arbitrary size, so no overflow issues.
        
        # Step 1: Create a number by XORing n with n right-shifted by 1.
        # If n has alternating bits, all adjacent bits are different.
        # The i-th bit of (n ^ (n >> 1)) is (i-th bit of n) ^ ((i+1)-th bit of n).
        # If bits are alternating, this XOR is always 1.
        # The result will be a number with all bits set to 1 up to the MSB of n.
        # e.g., n = 5 (101) -> n ^ (n>>1) = 101 ^ 010 = 111
        # e.g., n = 10 (1010) -> n ^ (n>>1) = 1010 ^ 0101 = 1111
        x = n ^ (n >> 1)
        
        # Step 2: Check if x consists of all 1s in its binary representation.
        # A number made of all 1s (like 3, 7, 15) is of the form 2^k - 1.
        # Such a number has the property that (x & (x + 1)) is 0.
        # e.g., x = 7 (111), x+1 = 8 (1000). (7 & 8) == 0.
        # For a number not made of all 1s:
        # e.g., from n = 7 (111), x = 4 (100).
        # x = 4, x+1 = 5. (4 & 5) == 4 != 0.
        return (x & (x + 1)) == 0

Binary Number with Alternating Bits in Java

class Solution {
    public boolean hasAlternatingBits(int n) {
        // In Java, integer overflow wraps around (two's complement), so using `int` would
        // also work. Using `long` is a bit clearer and avoids reasoning about overflow.
        long num = n;

        // Step 1: Create a number by XORing n with n right-shifted by 1.
        // If n has alternating bits, all adjacent bits are different.
        // The i-th bit of (n ^ (n >> 1)) is (i-th bit of n) ^ ((i+1)-th bit of n).
        // If bits are alternating, this XOR is always 1.
        // The result will be a number with all bits set to 1 up to the MSB of n.
        // Example: n = 5 (101) -> num ^ (num >> 1) = 101 ^ 010 = 111 (7)
        long x = num ^ (num >> 1);

        // Step 2: Check if x consists of all 1s in its binary representation.
        // A number made of all 1s (like 3, 7, 15) is of the form 2^k - 1.
        // Such a number has the property that (x & (x + 1)) is 0.
        // Example: x = 7 (111), x + 1 = 8 (1000). 111 & 1000 = 0.
        // For a number not made of all 1s:
        // Example: n = 7 (111) -> x = 4 (100)
        // x = 4 (100), x + 1 = 5 (101). 100 & 101 = 100 != 0.
        return (x & (x + 1)) == 0;
    }
}

Binary Number with Alternating Bits in C++

class Solution {
public:
    bool hasAlternatingBits(int n) {
        // To avoid any potential issues with signed integer overflow, which is
        // undefined behavior in C++, we cast n to a larger unsigned type. 
        // `long long` is a safe choice.
        long long num = n;

        // Step 1: Create a number where bits are set if adjacent bits in n are different.
        // If all bits of n are alternating, n ^ (n>>1) will result in a number
        // that has all its significant bits set to 1.
        // e.g., n = 5 (101) -> num ^ (num>>1) = 101 ^ 010 = 111
        // e.g., n = 10 (1010) -> num ^ (num>>1) = 1010 ^ 0101 = 1111
        long long x = num ^ (num >> 1);
        
        // Step 2: Check if x is of the form 2^k - 1 (all bits are 1s).
        // A number with all bits set to 1 (like 7 (111) or 15 (1111))
        // has the property that (x & (x + 1)) == 0.
        // e.g., x = 7 (111), x+1 = 8 (1000). 111 & 1000 = 0.
        // If x is not all 1s (e.g., from n=7, x=4 (100)), the check fails:
        // x = 4 (100), x+1 = 5 (101). 100 & 101 = 100 != 0.
        return (x & (x + 1)) == 0;
    }
};

Binary Number with Alternating Bits in TypeScript

function hasAlternatingBits(n: number): boolean {
    // JavaScript numbers are 64-bit floats, but bitwise operations
    // are performed on 32-bit signed integers. Overflow wraps around.
    // The logic holds perfectly.

    // Step 1: Create a number by XORing n with n right-shifted by 1.
    // If n has alternating bits, all adjacent bits are different.
    // The i-th bit of (n ^ (n >> 1)) is (i-th bit of n) ^ ((i+1)-th bit of n).
    // If bits are alternating, this XOR is always 1.
    // The result will be a number with all bits set to 1 up to the MSB of n.
    // Example: n = 5 (101) -> n ^ (n >> 1) = 101 ^ 010 = 111 (7)
    let x = n ^ (n >> 1);

    // Step 2: Check if x consists of all 1s in its binary representation.
    // A number made of all 1s (like 3, 7, 15) is of the form 2^k - 1.
    // Such a number has the property that (x & (x + 1)) is 0.
    // Example: x = 7 (111), x + 1 = 8 (1000). 7 & 8 = 0.
    // For a number not made of all 1s:
    // Example: from n = 7 (111) -> x = 4 (100)
    // x = 4, x + 1 = 5. 4 & 5 = 4 != 0.
    return (x & (x + 1)) === 0;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.