Bird
Raised Fist0
Interview Prepbit-manipulationeasyAmazonGoogle

Power of Two

Choose your preparation mode4 modes available
</>
IDE
def isPowerOfTwo(n: int) -> bool:public boolean isPowerOfTwo(int n)bool isPowerOfTwo(int n)function isPowerOfTwo(n)
def isPowerOfTwo(n: int) -> bool:
    # Write your solution here
    pass
class Solution {
    public boolean isPowerOfTwo(int n) {
        // Write your solution here
        return false;
    }
}
#include <vector>
using namespace std;

bool isPowerOfTwo(int n) {
    // Write your solution here
    return false;
}
function isPowerOfTwo(n) {
    // Write your solution here
}
Coming soon
0/10
Common Bugs to Avoid
Wrong: true for 0Missing check for n <= 0 before bit manipulation.✅ Add 'if n <= 0: return False' before bitwise check.
Wrong: true for negative numbers like -16 or -1Not excluding negative inputs before bitwise operations.✅ Add 'if n <= 0: return False' to exclude negatives.
Wrong: true for numbers with multiple bits set like 3 or 2147483647Incorrect bitwise condition; not using n & (n-1) == 0.✅ Use 'return n > 0 and (n & (n-1)) == 0' to check power of two.
Wrong: false for 1 or 2Off-by-one error or incorrect base case handling.✅ Ensure 1 and 2 return true by applying correct bitwise condition.
Wrong: TLE on large inputsUsing loop or recursion instead of O(1) bit manipulation.✅ Replace loop with 'return n > 0 and (n & (n-1)) == 0' for constant time.
✓
Test Cases
t1_01basic
Input1
Expectedtrue

1 is 2^0, so it is a power of two.

t1_02basic
Input16
Expectedtrue

16 is 2^4, so it is a power of two.

t2_01edge
Input0
Expectedfalse

0 is not a power of two by definition.

t2_02edge
Input-16
Expectedfalse

Negative numbers are not powers of two.

t2_03edge
Input2147483647
Expectedfalse

2147483647 (2^31 - 1) is not a power of two.

t2_04edge
Input1
Expectedtrue

1 is the exact boundary case (2^0) and must return true.

t3_01corner
Input3
Expectedfalse

3 (binary 11) has two bits set, so not a power of two.

t3_02corner
Input-1
Expectedfalse

-1 in two's complement has all bits set, not a power of two.

t3_03corner
Input2
Expectedtrue

2 is 2^1, a power of two.

t4_01performance
Input{"_description":"n=100000 at constraint boundary - executor generates this"}
⏱ Performance - must finish in 2000ms

Input size n=100000 tests O(1) bit manipulation performance within 2 seconds.

Practice

(1/5)
1. Consider the following code that generates all permutations of 3 elements using bitmask DP with iterative tabulation. What is the content of dp[7] after the loops finish?

def generate_permutations_iterative(n):
    dp = [[] for _ in range(1 << n)]
    dp[0] = [[]]
    for mask in range(1 << n):
        for i in range(n):
            if not (mask & (1 << i)):
                for perm in dp[mask]:
                    dp[mask | (1 << i)].append(perm + [i])
    return dp[(1 << n) - 1]

perms = generate_permutations_iterative(3)
print(perms)
easy
A. [[0,1,2], [0,2,1], [1,0,2], [1,2,0], [2,0,1], [2,1,0]]
B. [[0,1,2], [1,0,2], [2,0,1]]
C. [[0,1,2], [0,2,1], [1,0,2], [2,1,0]]
D. [[0,1,2], [1,2,0], [2,1,0], [2,0,1]]

Solution

  1. Step 1: Trace dp[0] initialization

    dp[0] starts with [[]], representing empty permutation.
  2. Step 2: Iteratively build dp for all masks

    For each mask, append unused elements to existing permutations, eventually filling dp[7] with all permutations of [0,1,2].
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    dp[7] contains all 3! = 6 permutations of [0,1,2] [OK]
Hint: dp[(1<
Common Mistakes:
  • Confusing mask indexing leading to missing permutations
  • Off-by-one errors in bitmask checks
2. What is the output of the following code when the input is 11?
def hammingWeight(n: int) -> int:
    return bin(n).count('1')

print(hammingWeight(11))
easy
A. 2
B. 4
C. 3
D. 1

Solution

  1. Step 1: Convert 11 to binary

    11 in binary is 1011.
  2. Step 2: Count '1's in '1011'

    There are three '1's in the string.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    bin(11) = '0b1011', count('1') = 3 [OK]
Hint: Count '1's in binary string representation [OK]
Common Mistakes:
  • Counting bits incorrectly
  • Confusing decimal and binary digits
3. Identify the bug in the following code snippet for bitwise AND of numbers range:
medium
A. The bit-clearing operation 'right = right & (right - 1)' is incorrect.
B. There is no bug; the code works correctly.
C. The function should return 'left' instead of 'right'.
D. The loop condition should be 'right > left' instead of 'right != left'.

Solution

  1. Step 1: Analyze loop condition

    The loop runs while right != left, which fails if right < left, causing infinite loop or incorrect behavior.
  2. Step 2: Correct condition

    Changing condition to 'right > left' ensures loop terminates when right reaches or goes below left.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Loop condition must prevent infinite loops and handle equality correctly [OK]
Hint: Loop condition must be 'right > left' to terminate properly [OK]
Common Mistakes:
  • Using '!=' instead of '>' causes infinite loops
  • Misunderstanding bit-clearing operation
  • Returning wrong variable
4. The following code attempts to find the missing number using XOR. Which line contains the subtle bug that causes incorrect results when the missing number is n?
medium
A. Line 3: initializing xor_all to 0
B. Line 7: returning xor_all without modification
C. Line 4: iterating only to n-1 instead of n
D. Line 6: XORing array elements

Solution

  1. Step 1: Analyze the loop range for XORing indices

    The loop runs from 0 to n-1 (range(n)), missing the number n itself.
  2. Step 2: Understand impact on missing number detection

    Since n is not XORed, if n is the missing number, it won't be accounted for, causing wrong output.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Changing loop to range(n+1) fixes the bug [OK]
Hint: Loop must include n to XOR all indices correctly [OK]
Common Mistakes:
  • Off-by-one in loop range
  • Forgetting to XOR n
  • Assuming initialization causes bug
5. What is the time complexity of generating an n-bit Gray code sequence using the optimal bitwise formula [i ^ (i >> 1) for i in range(1 << n)]?
medium
A. O(n)
B. O((2^n)!)
C. O(n * 2^n)
D. O(2^n)

Solution

  1. Step 1: Analyze the loop iteration count

    The list comprehension iterates over all integers from 0 to 2^n - 1, so 2^n iterations.
  2. Step 2: Analyze work per iteration

    Each iteration performs constant time bitwise operations (XOR and right shift), so O(1) per iteration.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Total time is proportional to 2^n, not multiplied by n or factorial [OK]
Hint: Loop runs 2^n times with O(1) work each [OK]
Common Mistakes:
  • Confusing n with 2^n and claiming O(n)
  • Assuming bitwise operations cost O(n)
  • Mistaking factorial complexity from brute force backtracking
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard