AlgoMaster Logo

Guess Number Higher or Lower

Last Updated: March 29, 2026

easy

Understanding the Problem

We need to find a secret number in the range [1, n] using only the guess API. Each call to guess(num) tells us one of three things: our guess is correct (returns 0), too high (returns -1), or too low (returns 1).

This is essentially a search problem. We're looking for a specific value in a sorted range, and we have a comparison oracle that tells us which direction to go. The range [1, n] is inherently sorted, which is a strong hint about the right technique.

The critical observation is that n can be as large as 2^31 - 1 (about 2.1 billion). Checking every number one by one would be painfully slow. But the sorted nature of the search space means we can eliminate half the candidates with each guess, and that's exactly what binary search does.

Key Constraints:

  • 1 <= n <= 2^31 - 1 → n can be up to about 2.1 billion. A linear scan would require up to 2.1 billion API calls, which is far too slow. We need O(log n), and log2(2^31) = 31, so binary search would need at most 31 guesses.
  • 1 <= pick <= n → The answer always exists within the range, so we're guaranteed to find it. No need to handle "not found" cases.

Approach 1: Linear Scan

Intuition

The simplest possible approach: just try every number from 1 to n until the API tells us we got it right. It's brute force, but it's guaranteed to find the answer since pick is somewhere in [1, n].

This is how you'd solve the problem if you had no cleverness at all. Start at 1, ask "is it 1?", then "is it 2?", and so on. Eventually you'll hit the right number.

Algorithm

  1. Start from number 1.
  2. Call guess(i) for each number i from 1 to n.
  3. If guess(i) returns 0, return i as the answer.

Example Walkthrough

Input: n = 10, pick = 6

We call guess(1), guess(2), ..., guess(6). On the sixth call, guess(6) returns 0 and we return 6. We made 6 API calls to find the answer.

Code

The linear scan ignores the "higher or lower" feedback entirely. What if we used that information to eliminate half the candidates with each guess?

Approach 2: Binary Search

Intuition

This problem is tailor-made for binary search. We have a sorted range [1, n], and the guess API acts as a comparator that tells us whether to go left or right. Instead of checking numbers one by one, we can guess the middle of our current range and instantly eliminate half the candidates.

Think about how you'd actually play this game with a friend. If the range is 1 to 100 and you guess 50, and they say "higher," you wouldn't start checking 51, 52, 53... You'd jump straight to 75, the midpoint of [51, 100]. Each guess cuts the problem in half.

With n up to 2^31 - 1, binary search needs at most 31 guesses. Compare that to 2 billion for the linear approach. That's the power of logarithmic search.

Algorithm

  1. Set low = 1 and high = n.
  2. While low is less than or equal to high:
    • Compute the midpoint: mid = low + (high - low) / 2.
    • Call guess(mid).
    • If the result is 0, return mid (we found the number).
    • If the result is -1, the pick is lower, so set high = mid - 1.
    • If the result is 1, the pick is higher, so set low = mid + 1.
  3. Return -1 (should never reach here since the pick is guaranteed to exist).

Example Walkthrough

1Initial range [1, 10]. Guess mid = 5
0
1
low
1
2
2
3
3
4
4
mid
5
5
6
6
7
7
8
8
9
9
10
high
1/6

Code