Data Structures & AlgorithmsModerateTCS NQT · Product Company OA

Problem: How many binary strings of length 3 contain an even number of 1s? Run the code.

Problem: How many binary strings of length 3 contain an even number of 1s? Run the code.
print(sum(1 for t in range(8) if bin(t).count('1') % 2 == 0))
What is printed?
  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 5

Correct answer: B. 4

The even-weight strings are 000, 011, 101 and 110, so the count is 4.

More Coding Interview Problems questions

Exams that ask this topic