Guessing a Number
Learn the main approach to solving a problem using the divide-and-conquer strategy.
We'll cover the following
Divide-and-conquer strategy
If we want to solve a problem using a divide-and-conquer strategy, we have to think about the following three steps:
- Breaking a problem into smaller subproblems.
- Solving each subproblem recursively.
- Combining a solution to the original problem out of solutions to subproblems.
The first two steps are the “divide” part, whereas the last step is the “conquer” part. We illustrate this approach with a number of problems of progressing difficulty and then proceed to the programming challenges.
Guess a number
In the “Guess a Number” game, our opponent has an integer in mind. We ask questions of the form “Is ?”. Our opponent replies either “yes”, or “” (that is, “my number is smaller than your guess”), or “” (that is, “my number is larger than your guess”). Our goal is to get the “yes” answer by asking the minimum number of questions. Let . Our goal is to guess by asking at most two questions. Can we do this? Let’s try it here (Level 1).
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.