0
0
DSA Pythonprogramming~20 mins

Check if Number is Power of Two in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Power of Two Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
ATrue
BFalse
C0
DTypeError
Attempts:
2 left
💡 Hint
Think about how binary AND works for powers of two.
Predict Output
intermediate
2: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))
ATrue
BNone
CFalse
DZeroDivisionError
Attempts:
2 left
💡 Hint
Check if 18 can be divided by 2 repeatedly until 1.
Predict Output
advanced
2: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))
ATrue
BFalse
CTypeError
D0
Attempts:
2 left
💡 Hint
Understand how n & (-n) isolates the lowest set bit.
🔧 Debug
advanced
2: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))
AFalse
BSyntaxError
CTypeError
DTrue
Attempts:
2 left
💡 Hint
Check operator precedence and parentheses.
🚀 Application
expert
2: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.
A11
B8
C9
D10
Attempts:
2 left
💡 Hint
List powers of two: 1, 2, 4, 8, ... up to 1000.