Data Structures & AlgorithmsModerateTCS NQT · CAT

Problem: The Hamming weight (number of 1-bits) of each integer from 0 to 15 is summed. What is the total?

  1. A. 32
  2. B. 64
  3. C. 24
  4. D. 15

Correct answer: A. 32

Each of the 4 bit positions is set in 8 of the 16 numbers, so the total is 4 × 8 = 32. 4 × 16 = 64 forgets the half, 3 bits give 24, and 15 counts the non-zero numbers.

More Coding Interview Problems questions

Exams that ask this topic