KatInterview

342. Power of Four

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

The problem asks us to determine if a given integer n is a power of four. A number is a power of four if it can be expressed as 4^x for some integer x. For example, 1 (4^0), 4 (4^1), 16 (4^2) are powers of four.

Solution

Power of Four in Python

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        # A number n is a power of four if it meets three conditions:
        # 1. It must be positive (n > 0).
        # 2. It must be a power of two. This can be checked by (n & (n - 1)) == 0.
        #    This ensures n has only one bit set to '1'.
        # 3. The single '1' bit must be at an even position (0, 2, 4, ...).
        #    This can be checked with a bitmask. The number 0x55555555 is a 32-bit number
        #    with '1's at all even positions (01010101...).
        #    If n is a power of four, n & 0x55555555 will be non-zero (equal to n itself).
        if n <= 0:
            return False
        
        # Check if it's a power of 2
        is_power_of_two = (n & (n - 1)) == 0
        if not is_power_of_two:
            return False
        
        # Check if the set bit is at an even position
        # The mask 0x55555555 has 1s at all even positions for a 32-bit integer.
        return (n & 0x55555555) != 0

Power of Four in Java

class Solution {
    public boolean isPowerOfFour(int n) {
        // A number n is a power of four if it meets three conditions:
        // 1. It must be positive (n > 0).
        // 2. It must be a power of two. This can be checked by (n & (n - 1)) == 0.
        //    This ensures n has only one bit set to '1'.
        // 3. The single '1' bit must be at an even position (0, 2, 4, ...).
        //    This can be checked with a bitmask. The number 0x55555555 is a 32-bit number
        //    with '1's at all even positions (01010101...).
        //    If n is a power of four, n & 0x55555555 will be non-zero (equal to n itself).
        return n > 0 && (n & (n - 1)) == 0 && (n & 0x55555555) != 0;
    }
}

Power of Four in C++

class Solution {
public:
    bool isPowerOfFour(int n) {
        // A number n is a power of four if it meets three conditions:
        // 1. It must be positive (n > 0).
        // 2. It must be a power of two. This can be checked by (n & (n - 1)) == 0.
        //    This ensures n has only one bit set to '1'.
        // 3. The single '1' bit must be at an even position (0, 2, 4, ...).
        //    This can be checked with a bitmask. The number 0x55555555 is a 32-bit number
        //    with '1's at all even positions (01010101...).
        //    If n is a power of four, n & 0x55555555 will be non-zero (equal to n itself).
        return n > 0 && (n & (n - 1)) == 0 && (n & 0x55555555) != 0;
    }
};

Power of Four in TypeScript

function isPowerOfFour(n: number): boolean {
    // A number n is a power of four if it meets three conditions:
    // 1. It must be positive (n > 0).
    // 2. It must be a power of two. This can be checked by (n & (n - 1)) == 0.
    //    This ensures n has only one bit set to '1'.
    // 3. The single '1' bit must be at an even position (0, 2, 4, ...).
    //    This can be checked with a bitmask. The number 0x55555555 is a 32-bit number
    //    with '1's at all even positions (01010101...).
    //    If n is a power of four, n & 0x55555555 will be non-zero (equal to n itself).
    //    Note: Bitwise operations in JS/TS work on 32-bit signed integers.
    return n > 0 && (n & (n - 1)) === 0 && (n & 0x55555555) !== 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.