Cred SWE Interview Question 2021 November | Bit Manipulation | on-campus

Cred · Question · Posted Jun 2026

Question 1: 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: 11 in binary is 00000000000000000000000000001011. It has three 1-bits (at positions 0, 1, and 3). The trick n & (n-1) clears the lowest set bit each ...

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

Log in Create a free account