Answer: Cloudflare OA Question 2024 September | Bit Manipulation | on-campus

Answer · Posted Jun 2026

Solution: Number of 1 Bits — Brian Kernighan's Bit Trick Approach We use Brian Kernighan's Algorithm. The key insight is the bit trick: n & (n - 1) always clears the lowest set bit of n. Subtracting 1 from n flips the lowest set bit and all bits below it. ANDing with the original n clears those flipped bits. We repeat this operation and count how many times it takes to reduce n to 0. Each iteration removes exactly one ...

The full answer & interview discussion are available to premium members.

Log in Create a free account