Data Structures & AlgorithmsModerateTCS NQT Digital · Accenture

Problem: A string is built by writing the first 15 positive integers in binary, concatenated in order (1, 10, 11, 100, ..., 1111). How many '1' characters appear in the first 40 characters of this string?

  1. A. 32
  2. B. 25
  3. C. 16
  4. D. 24

Correct answer: D. 24

Numbers 1..7 take 17 characters and 8..12 take 20 more, so the first 40 characters are 1..12 (22 ones) plus '110' from 13 = 1101, giving 24. Counting the whole 49-character string gives 32, reading 41 characters gives 25, and counting zeros gives 16.

More Coding Interview Problems questions

Exams that ask this topic