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 for n = 16?
DSA C
int isPowerOfTwo(unsigned int n) { return n && !(n & (n - 1)); } #include <stdio.h> int main() { unsigned int n = 16; if (isPowerOfTwo(n)) { printf("Yes\n"); } else { printf("No\n"); } return 0; }
Attempts:
2 left
💡 Hint
A power of two has exactly one bit set in binary.
✗ Incorrect
The function returns true if n is not zero and n & (n-1) is zero, which is true only for powers of two. 16 is 2^4, so output is 'Yes'.
❓ Predict Output
intermediate2:00remaining
What is the output when n = 0?
Using the same function, what will be printed for n = 0?
DSA C
int isPowerOfTwo(unsigned int n) { return n && !(n & (n - 1)); } #include <stdio.h> int main() { unsigned int n = 0; if (isPowerOfTwo(n)) { printf("Yes\n"); } else { printf("No\n"); } return 0; }
Attempts:
2 left
💡 Hint
Zero is not a power of two.
✗ Incorrect
The function returns false for zero because the first condition 'n' fails. So output is 'No'.
🔧 Debug
advanced2:00remaining
What error does this code raise?
Identify the error in this code snippet that attempts to check if n is a power of two.
DSA C
int isPowerOfTwo(unsigned int n) { return n & (n - 1) == 0; } #include <stdio.h> int main() { unsigned int n = 8; if (isPowerOfTwo(n)) { printf("Yes\n"); } else { printf("No\n"); } return 0; }
Attempts:
2 left
💡 Hint
Operator precedence matters in bitwise and comparison operations.
✗ Incorrect
The expression 'n & (n - 1) == 0' is evaluated as 'n & ((n - 1) == 0)', which is wrong. It should be '(n & (n - 1)) == 0'.
🧠 Conceptual
advanced1:00remaining
How many numbers between 1 and 16 (inclusive) are powers of two?
Count how many numbers in the range 1 to 16 are powers of two.
Attempts:
2 left
💡 Hint
Powers of two are 1, 2, 4, 8, 16 in this range.
✗ Incorrect
The powers of two in 1 to 16 are 1, 2, 4, 8, and 16, total 5 numbers.
🚀 Application
expert3:00remaining
What is the value of variable 'count' after this code runs?
This code counts how many numbers in an array are powers of two. What is the final count?
DSA C
#include <stdio.h> int isPowerOfTwo(unsigned int n) { return n && !(n & (n - 1)); } int main() { unsigned int arr[] = {3, 4, 6, 8, 16, 18, 32}; int count = 0; for (int i = 0; i < 7; i++) { if (isPowerOfTwo(arr[i])) { count++; } } printf("%d\n", count); return 0; }
Attempts:
2 left
💡 Hint
Check each number if it has exactly one bit set.
✗ Incorrect
Numbers 4, 8, 16, and 32 are powers of two, so count is 4.
