LeetCode Problem

How to Solve Guess Number Higher or Lower

This problem requires identifying a secret number using the least number of guesses by applying a binary search over the valid range. Each guess receives immediate higher or lower feedback, allowing you to narrow down the search space efficiently. The key is maintaining low and high bounds and adjusting them correctly until the target number is found, ensuring O(log n) steps in the worst case.

GhostInterview Help

Need help with Guess Number Higher or Lower without spending extra time grinding it?

GhostInterview can read Guess Number Higher or Lower from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #374Binary search over the valid answer spaceReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Binary search over the valid answer space
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem requires identifying a secret number using the least number of guesses by applying a binary search over the valid range. Each guess receives immediate higher or lower feedback, allowing you to narrow down the search space efficiently. The key is maintaining low and high bounds and adjusting them correctly until the target number is found, ensuring O(log n) steps in the worst case.

Problem Statement

You are playing a guessing game where a number between 1 and n is selected, and your task is to identify it with minimal guesses. Each time you make a guess, you receive feedback indicating whether the secret number is higher, lower, or equal to your guess.

Implement a function that interacts with the guess API to determine the picked number. Your solution must efficiently reduce the search space using the feedback, leveraging the binary search pattern to achieve optimal guesses for any valid n.

Examples

Example 1

Input: n = 10, pick = 6

Output: 6

Example details omitted.

Example 2

Input: n = 1, pick = 1

Output: 1

Example details omitted.

Example 3

Input: n = 2, pick = 1

Output: 1

Example details omitted.

Constraints

  • 1 <= n <= 231 - 1
  • 1 <= pick <= n

Solution Approach

Initialize Search Bounds

Start with low = 1 and high = n. These bounds define the current search space, ensuring each guess halves the remaining possibilities.

Binary Search Iteration

While low is less than or equal to high, guess the midpoint. Use the feedback from the guess API to update low or high, shrinking the search space toward the picked number.

Return the Picked Number

Once the feedback indicates a correct guess, return that number. This guarantees you find the target efficiently, avoiding unnecessary guesses and leveraging the logarithmic nature of binary search.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(log n) since each guess reduces the search space by half. Space complexity is O(1) because only the low, high, and mid variables are stored.

What Interviewers Usually Probe

  • Expect you to explain why binary search fits this interactive guessing scenario.
  • They may ask about edge cases like n = 1 or pick at the boundaries.
  • Interviewers often check whether you handle integer overflow when computing mid = low + (high - low)/2.

Common Pitfalls or Variants

Common pitfalls

  • Using (low + high)/2 directly can cause integer overflow for large n.
  • Not updating low or high correctly based on the guess feedback leads to infinite loops.
  • Failing to consider that pick could be at the initial bounds causes off-by-one errors.

Follow-up variants

  • Modify the game to allow a range of valid picks and find any number in that range.
  • Implement a version where guesses have a cost, requiring minimal total guess cost.
  • Adapt the binary search approach to a descending ordered pick range instead of ascending.

How GhostInterview Helps

  • Provides step-by-step binary search guidance tailored to interactive guess problems.
  • Highlights potential pitfalls like integer overflow and off-by-one errors before they occur.
  • Offers practice scenarios matching the exact feedback-driven narrowing pattern in this problem.

Topic Pages

FAQ

What is the best strategy to solve Guess Number Higher or Lower?

Applying a binary search over the valid range ensures the minimal number of guesses and efficiently narrows the search space.

Can the picked number be at the bounds?

Yes, pick can be 1 or n, so your binary search must handle edge cases correctly without skipping bounds.

Why is using (low + high)/2 potentially dangerous?

It may overflow for large n values; instead, compute mid as low + (high - low)/2 to stay safe.

Does the algorithm require extra memory?

No, the search maintains only low, high, and mid variables, giving O(1) space complexity.

How does GhostInterview assist with this binary search pattern?

It guides you through adjusting search bounds interactively, ensuring correct application of the binary search pattern and avoiding common off-by-one errors.

GhostInterview Solver

Need direct help with Guess Number Higher or Lower instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Guess Number Higher or Lower from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.