Guess Number Higher or Lower
EasyBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.A secret number pick has been chosen from 1 to n. You cannot see it, but you are given a function guess(num) that tells you how your guess compares:
-1if your guess is too high (pick < num)1if your guess is too low (pick > num)0if you got it
Write guess_number(n, guess) (TypeScript: guessNumber(n, guess)) that returns pick. The tests count your calls, so use as few as possible.
n = 10, pick = 6 → 6
n = 1, pick = 1 → 1
n = 2, pick = 1 → 1Constraints
1 <= n <= 2^31 - 11 <= pick <= n