Challenge - 5 Problems
Set Bits Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Brian Kernighan's Algorithm for Counting Set Bits
What is the output of the following C code that counts set bits using Brian Kernighan's algorithm?
DSA C
int countSetBits(int n) { int count = 0; while (n) { n = n & (n - 1); count++; } return count; } int main() { int num = 29; int result = countSetBits(num); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Count how many times the loop runs by clearing the rightmost set bit each time.
✗ Incorrect
The number 29 in binary is 11101, which has 4 set bits. Brian Kernighan's algorithm clears one set bit per iteration, so the loop runs 4 times.
❓ Predict Output
intermediate2:00remaining
Result of Counting Set Bits for Zero Input
What will be the output when the input number is zero in the Brian Kernighan's set bit counting function?
DSA C
int countSetBits(int n) { int count = 0; while (n) { n = n & (n - 1); count++; } return count; } int main() { int num = 0; int result = countSetBits(num); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Consider how many set bits zero has.
✗ Incorrect
Zero has no set bits, so the loop never runs and the count remains zero.
🔧 Debug
advanced2:00remaining
Identify the Error in Brian Kernighan's Algorithm Implementation
What error will the following code produce when compiled and run?
DSA C
int countSetBits(int n) { int count = 0; while (n != 0) { n = n & n - 1; count++; } return count; } int main() { int num = 15; int result = countSetBits(num); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Operator precedence: subtraction (-) has higher precedence than bitwise AND (&).
✗ Incorrect
The expression `n & n - 1` is parsed as `n & (n - 1)` because subtraction has higher precedence than bitwise AND. The code runs correctly, clearing one set bit per iteration. 15 in binary is 1111, which has 4 set bits, so it outputs 4.
❓ Predict Output
advanced2:00remaining
Output of Counting Set Bits for a Large Number
What is the output of the following code when counting set bits of 1023 using Brian Kernighan's algorithm?
DSA C
int countSetBits(int n) { int count = 0; while (n) { n = n & (n - 1); count++; } return count; } int main() { int num = 1023; int result = countSetBits(num); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
1023 in binary is all ones in the first 10 bits.
✗ Incorrect
1023 is 2^10 - 1, which means its binary representation has 10 set bits.
🧠 Conceptual
expert2:00remaining
Why Brian Kernighan's Algorithm is Efficient for Counting Set Bits
Which statement best explains why Brian Kernighan's algorithm is more efficient than checking each bit individually?
Attempts:
2 left
💡 Hint
Think about how many times the loop runs compared to total bits.
✗ Incorrect
Brian Kernighan's algorithm loops only as many times as there are set bits, skipping zero bits, making it faster especially for numbers with few set bits.
