Guess Number Higher or Lower - Problem

Welcome to the classic Guess the Number game! ๐ŸŽฏ

In this interactive puzzle, a secret number has been chosen from the range 1 to n. Your mission is to find this hidden number using the minimum number of guesses possible.

How it works:

  • You make a guess by calling the API function guess(num)
  • The API returns helpful feedback:
    • -1: Your guess is too high (the secret number is lower)
    • 1: Your guess is too low (the secret number is higher)
    • 0: Bingo! You found the secret number

Goal: Return the secret number that was chosen.

Challenge: Can you solve it efficiently without trying every possible number?

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 10, pick = 6
โ€บ Output: 6
๐Ÿ’ก Note: We need to find the number 6 in the range 1-10. Using binary search: guess(5)โ†’1 (too low), guess(8)โ†’-1 (too high), guess(6)โ†’0 (found!). Return 6.
example_2.py โ€” Edge Case Low
$ Input: n = 1, pick = 1
โ€บ Output: 1
๐Ÿ’ก Note: Only one possible number. First guess(1) returns 0, so we immediately return 1.
example_3.py โ€” Edge Case High
$ Input: n = 2, pick = 2
โ€บ Output: 2
๐Ÿ’ก Note: Two possible numbers. Using binary search: guess(1)โ†’1 (too low), guess(2)โ†’0 (found!). Return 2.

Visualization

Tap to expand
Binary Search: The Optimal StrategyComparison: Linear vs Binary Search (n=16)Linear Search: 1 โ†’ 2 โ†’ 3 โ†’ 4 โ†’ 5 โ†’ 6 โ†’ 7 โ†’ 8 โ†’ 9 โ†’ 10 (found!)Worst case: 16 guessesBinary Search: 8 โ†’ 12 โ†’ 10 โ†’ 9 (found!)Worst case: 4 guesses = โŒˆlogโ‚‚(16)โŒ‰Visual Representation:Initial Range: 1 to 16After guess(8): 1 to 8After guess(12): 9 to 12After guess(10): 9 to 109 โœ“Growth Comparison:n = 100:Linear: up to 100 guessesBinary: max 7 guessesn = 1,000:Linear: up to 1,000 guessesBinary: max 10 guessesn = 1,000,000:Linear: up to 1,000,000 guessesBinary: max 20 guesses!๐ŸŽฏ Key Insight: Binary Search EfficiencyEach guess eliminates exactly half of the remaining possibilities,guaranteeing we find the answer in O(log n) time regardless of the target!
Understanding the Visualization
1
Start in the Middle
Always guess the middle number of your current range
2
Eliminate Half
Based on 'too high' or 'too low', eliminate half the remaining possibilities
3
Repeat
Continue halving the search space until you find the answer
4
Guaranteed Success
You'll find any number in at most โŒˆlogโ‚‚(n)โŒ‰ guesses
Key Takeaway
๐ŸŽฏ Key Insight: Binary search transforms a potentially massive search into a logarithmic problem, making it feasible to search even billions of numbers in just ~30 guesses!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(log n)

Each guess eliminates half of the remaining search space, so we need at most logโ‚‚(n) guesses

n
2n
โšก Linearithmic
Space Complexity
O(1)

We only need a few variables to track the left and right boundaries

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 231 - 1
  • 1 โ‰ค pick โ‰ค n
  • Important: The guess API is provided and you cannot modify it
  • You should minimize the number of calls to the guess API
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
89.6K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen