Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the total number of set bits in the binary representations of 1 through 100?

Problem: What is the total number of set bits in the binary representations of 1 through 100?
print(sum(bin(i).count('1') for i in range(1, 101)))
What is printed?
  1. A. 316
  2. B. 319
  3. C. 350
  4. D. 448

Correct answer: B. 319

Summing popcounts over 1..100 gives 319. Using range(1, 100) drops 100 = 1100100 (3 bits) and gives 316, assuming each of 7 bit positions is set half the time gives 100 × 7 / 2 = 350, and the exact formula 7 × 2⁶ = 448 holds only for 0..127.

More Coding Interview Problems questions

Exams that ask this topic