Guess Number Higher or Lower — Interactive & Iterators
Find a hidden value within a known range by asking a limited number of questions, each reply saying higher, lower or correct.
Interactive & Iterators · Easy
The problem
This is a game of twenty questions played with numbers: a friend has thought of a secret and answers each of your guesses with a direction. A secret number between 1 and n has been picked. A provided function guess(num) compares your number to the secret and tells you which way to go; calls are limited. Find the secret number.
The story
A friend picks a secret number from 1 to n, with n at most 100. Each guess you make gets an answer: -1 if your guess is too high, 1 if it's too low, and 0 if it's right. You get at most 9 guesses. Find the secret.
What to return
- Return the secret number.
- guess(num) returns -1 if num is higher than the secret, 1 if num is lower than the secret, and 0 if num is the secret.
- You may call guess at most 9 times per test; one more call makes the test fail.
Constraints
- 1 <= n <= 100
- the secret is an integer in 1..n
Example
- Input
n = 12- Output
9