Challenge - 5 Problems
Power of Two Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code snippet?
Given the function to check if a number is a power of two, what will be printed after calling
print(is_power_of_two(16))?DSA Python
def is_power_of_two(n): return n > 0 and (n & (n - 1)) == 0 print(is_power_of_two(16))
Attempts:
2 left
💡 Hint
Think about how binary AND works for powers of two.
✗ Incorrect
A number that is a power of two has exactly one bit set in binary. The expression n & (n-1) clears the lowest set bit. If the result is zero, n was a power of two.
❓ Predict Output
intermediate2:00remaining
What is the output of this code snippet?
What will be printed after running this code?
DSA Python
def is_power_of_two(n): if n <= 0: return False while n % 2 == 0: n = n // 2 return n == 1 print(is_power_of_two(18))
Attempts:
2 left
💡 Hint
Check if 18 can be divided by 2 repeatedly until 1.
✗ Incorrect
18 is divisible by 2 once (18/2=9), but 9 is not divisible by 2, so the function returns False.
❓ Predict Output
advanced2:00remaining
What is the output of this code snippet?
What will be printed after running this code?
DSA Python
def is_power_of_two(n): return n > 0 and (n & (-n)) == n print(is_power_of_two(32))
Attempts:
2 left
💡 Hint
Understand how n & (-n) isolates the lowest set bit.
✗ Incorrect
For powers of two, n & (-n) equals n itself, so the function returns True.
🔧 Debug
advanced2:00remaining
What error does this code raise?
What error will occur when running this code?
DSA Python
def is_power_of_two(n): return n > 0 and (n & (n-1)) == 0 print(is_power_of_two(8))
Attempts:
2 left
💡 Hint
Check operator precedence and parentheses.
✗ Incorrect
The expression (n & n-1) is parsed as (n & n) - 1 which is invalid syntax without parentheses.
🚀 Application
expert2:00remaining
How many numbers between 1 and 1000 (inclusive) are powers of two?
Count how many integers from 1 to 1000 are powers of two.
Attempts:
2 left
💡 Hint
List powers of two: 1, 2, 4, 8, ... up to 1000.
✗ Incorrect
Powers of two up to 1000 are 1,2,4,8,16,32,64,128,256,512 (10 numbers).