Skip to content

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:

  • -1 if your guess is too high (pick < num)
  • 1 if your guess is too low (pick > num)
  • 0 if 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   →  1

Constraints

  • 1 <= n <= 2^31 - 1
  • 1 <= pick <= n