# Solving Leetcode Interviews in Seconds with AI: Guess Number Higher or Lower


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "374" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess. You call a pre-defined API int guess(int num), which returns three possible results:  -1: Your guess is higher than the number I picked (i.e. num > pick). 1: Your guess is lower than the number I picked (i.e. num < pick). 0: your guess is equal to the number I picked (i.e. num == pick).  Return the number that I picked.   Example 1:  Input: n = 10, pick = 6 Output: 6  Example 2:  Input: n = 1, pick = 1 Output: 1  Example 3:  Input: n = 2, pick = 1 Output: 1    Constraints:  1 <= n <= 231 - 1 1 <= pick <= n  

	# Explanation
	Here's an efficient solution to the Guess Game problem:

*   **Binary Search:** The core idea is to use binary search to efficiently narrow down the search range. We repeatedly halve the interval until we find the correct number.
*   **Adjust Search Range:** Based on the result of the `guess()` API, we adjust the search range. If the guess is too high, we reduce the upper bound. If it's too low, we increase the lower bound.

*   **Runtime & Storage Complexity:** O(log n) runtime, O(1) storage.

	
	# Code
	```python
	def guessNumber(n: int) -> int:
    """
    Implements the Guess Game using binary search.

    Args:
        n: The upper bound of the range of numbers to guess from (1 to n).

    Returns:
        The number that was picked.
    """
    low = 1
    high = n

    while low <= high:
        mid = low + (high - low) // 2  # Prevent potential overflow
        result = guess(mid)

        if result == 0:
            return mid
        elif result == -1:
            high = mid - 1
        else:
            low = mid + 1

    return -1  # Should not reach here based on problem constraints

# Dummy guess function for local testing, to be replaced with the actual API call
def guess(num: int) -> int:
    pick = 6 # Replace with your 'pick' value for local testing
    if num > pick:
        return -1
    elif num < pick:
        return 1
    else:
        return 0
	```
			
