> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Guess Number Higher or Lower Python Solution

> Tested Python solution for LeetCode 374 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 374, Easy. Topics: Binary Search, Interactive. [View on LeetCode](https://leetcode.com/problems/guess-number-higher-or-lower/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 374   # by problem number
lcpy gen -s guess_number_higher_or_lower   # by problem name
```

## Problem

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*.

### Examples

```
Input: n = 10, pick = 6
Output: 6
```

```
Input: n = 1, pick = 1
Output: 1
```

```
Input: n = 2, pick = 1
Output: 1
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/guess_number_higher_or_lower/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/guess_number_higher_or_lower/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
def guess(num: int) -> int:
    # Predefined by LeetCode; injected by tests.
    raise NotImplementedError


class Solution:
    # Time: O(log n)
    # Space: O(1)
    def guess_number(self, n: int) -> int:
        low = 1
        high = n
        while low <= high:
            mid = low + (high - low) // 2
            result = guess(mid)
            if result == 0:
                return mid
            if result < 0:
                high = mid - 1
            else:
                low = mid + 1
        return -1
```

## Complexity

| Time     | Space |
| -------- | ----- |
| O(log n) | O(1)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
