Data Structures & AlgorithmsModerateTCS NQT · logic

Problem: A player picks a number from 1 to 10 and the other player must guess it by yes/no questions. The minimum worst-case number of questions needed is what?

  1. A. 4
  2. B. 3
  3. C. 5
  4. D. 10

Correct answer: A. 4

Each yes/no question halves the candidate set, and 2^3 = 8 < 10 <= 16 = 2^4, so 4 questions are needed in the worst case. A binary search over 10 values takes ceil(log2 10) = 4 steps.

More Coding Interview Problems questions

Exams that ask this topic