Cloudflare OA Question 2024 September | Bit Manipulation | on-campus
Question · Posted Jun 2026
Question: Number of 1 Bits Problem Statement Write a function that takes the binary representation of a positive integer and returns the number of set bits it has (also known as the Hamming weight). Constraints 1 <= n <= 231 - 1 Examples Example 1: Input: n = 11 Output: 3 Explanation 1: The binary representation of 11 is 1011. It has three '1' bits. Each time we do n & (n-1), one set bit is cleared: 1011 → 1010 ...
The full answer & interview discussion are available to premium members.
Log in Create a free account