Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 200] have an even number of 1-bits in their binary form? Run the code.

Problem: How many integers in [1, 200] have an even number of 1-bits in their binary form? Run the code.
print(sum(1 for x in range(1, 201) if bin(x).count('1') % 2 == 0))
What is printed?
  1. A. 101
  2. B. 99
  3. C. 100
  4. D. 28

Correct answer: B. 99

Of 1..200, 99 have even popcount and 101 odd. 101 swaps the parity, 100 assumes an exact half (or counts 0), and 28 counts numbers with exactly two 1-bits.

More Coding Interview Problems questions

Exams that ask this topic