Data Structures & AlgorithmsHardCAT · Product Company SDE

Problem: How many integers x in the range 1 ≤ x ≤ 1000 have exactly 4 set bits in their binary representation?

  1. A. 120
  2. B. 385
  3. C. 210
  4. D. 252

Correct answer: C. 210

Every number from 1000 to 1023 starts with 11111 and has at least 5 set bits, so the count equals the 10-bit count C(10, 4) = 210. Counting exactly 3 bits gives C(10, 3) = 120, exactly 5 bits gives 252, and 'at most 4 bits' gives 385.

More Coding Interview Problems questions

Exams that ask this topic