Skip to content

Practice Guess Number Higher or Lower

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#374
LeetCode Problem

Guess Number Higher or Lower

Problem Statement

A fixed secret number is chosen from 1 through n. Find and return it using the provided API int guess(int num). The API returns -1 when num is greater than the secret, 1 when num is less than the secret, and 0 when they are equal.

Source: Guess Number Higher or Lower on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 10, pick = 6
Output
6
Explanation

With inclusive bounds [1,10], binary search first tries 5. The API returns 1, meaning the secret is larger, so the bounds become [6,10]. Trying 8 returns -1 and narrows them to [6,7]. Trying 6 returns 0, so return 6. The example reveals pick only to explain the test; the solution must learn it through guess calls. Be careful with the signs: -1 means the guess is too large, not that the secret should be searched for in the upper half.

# Example 2

Input
n = 1, pick = 1
Output
1
Explanation

The initial interval [1,1] contains only the number 1. Guessing 1 returns 0, so the answer is 1. There is no smaller positive candidate to test. This case checks that a binary-search loop still handles an interval containing a single value rather than stopping before that candidate is examined.

# Example 3

Input
n = 2, pick = 1
Output
1
Explanation

For inclusive bounds [1,2], the lower midpoint is 1. The API returns 0 immediately because the secret is 1, so the answer is 1. No second call is needed. The search range starts at 1, not 0; keeping that boundary avoids querying a number outside the stated range.

Constraints

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

How to solve Guess Number Higher or Lower

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Each guess call compares against a hidden position in the sorted range 1 through n, so this is binary search on the answer: every call halves the candidate interval, and about 31 calls suffice even when n is 2^31 - 1.

Why brute force is not enough

Guess 1, 2, 3, and so on until the API returns 0. With n up to 2^31 - 1 that can take over two billion calls, far beyond any interactive budget.

Approach

  1. Maintain an inclusive candidate interval, initially 1 through n, with the invariant that the secret lies inside it.
  2. Pick the midpoint as the low end plus half the width, so that adding two values near 2^31 cannot overflow a 32-bit integer.
  3. Call the API on the midpoint. A 0 ends the search; -1 means the guess is too high, so keep only the values below it; 1 means it is too low, so keep only the values above it.
  4. Repeat while the interval is non-empty. Since the secret exists, the loop ends at a 0 result after shrinking the interval by at least half on every call.

Walkthrough of Example 1

n = 10, pick = 6 → 6

  1. Start with the interval 1 through 10. The midpoint is 5, and guess(5) returns 1, so the secret is larger; the interval becomes 6 through 10.
  2. The midpoint of 6 through 10 is 8. guess(8) returns -1, so the secret is smaller; the interval becomes 6 through 7.
  3. The midpoint of 6 through 7, rounding down, is 6, and guess(6) returns 0.
  4. Return 6, the expected output, after three API calls.

Time and space

Time O(log n) · Space O(1). Every call discards at least half of the remaining candidates, so at most about log2(n) + 1 calls occur, roughly 31 for the largest n; only the two interval bounds are stored.

Interview pitfalls

  • Computing the midpoint as (low + high) / 2 overflows a signed 32-bit integer when both bounds approach 2^31 - 1; use low + (high - low) / 2.
  • Reversing the sign convention: -1 means your guess is too high, so the upper bound moves down, not the lower bound up.
  • Using an exclusive upper bound with n = 2^31 - 1 requires representing n + 1, which does not fit in a 32-bit signed integer.
  • Stopping when low equals high without guessing that last value misses the single-candidate case, such as n = 1.

Follow-up questions interviewers ask

  • How would you adapt the search if each call cost the number guessed and you had to minimize the worst-case total spent?
  • What if n is unknown and you only know the secret is a positive integer? Discuss exponential search to find an upper bound first.
  • Can you argue that no strategy can beat about log2(n) calls in the worst case?

Further reading on the technique: Binary search algorithm

Related LeetCode Problems

Practice more problems similar to Guess Number Higher or Lower.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.