Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: What is the number of set bits in 2^30 - 1? Run the code.

Problem: What is the number of set bits in 2^30 - 1? Run the code.
print(bin(2 ** 30 - 1).count('1'))
What is printed?
  1. A. 31
  2. B. 29
  3. C. 1
  4. D. 30

Correct answer: D. 30

2³⁰ − 1 is thirty 1s in binary (111…1), so popcount = 30. 31 counts the bits of 2³⁰, 29 is an off-by-one on the exponent, and 1 is the popcount of 2³⁰ itself.

More Coding Interview Problems questions

Exams that ask this topic