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

# First Bad Version Python Solution with Tests

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

LeetCode 278, Easy. Topics: Binary Search, Interactive. [View on LeetCode](https://leetcode.com/problems/first-bad-version/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 278   # by problem number
lcpy gen -s first_bad_version   # by problem name
```

## Problem

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have `n` versions `[1, 2, ..., n]` and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API `bool isBadVersion(version)` which returns whether `version` is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

### Examples

```
Input: n = 5, bad = 4
Output: 4
```

**Explanation:**

```
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
```

Then 4 is the first bad version.

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

### Constraints

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

**Note:** The `isBadVersion` API is already defined for you.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    def __init__(self, first_bad: int = 1) -> None:
        self.is_bad_version = lambda version: version >= first_bad

    # Time: O(log n)
    # Space: O(1)
    def first_bad_version(self, n: int) -> int:
        left = 1
        right = n

        while left < right:
            mid = (left + right) // 2
            if self.is_bad_version(mid):
                right = mid
            else:
                left = mid + 1

        return right


# BISECT PATTERNS - General Binary Search
# Given: arr = [10,20,30,30,30,40,50], target = 30
#               0  1  2  3  4  5  6
#
# bisect_left: Find FIRST occurrence (leftmost insertion point)
#   while left < right:
#       if arr[mid] >= target:  # >= keeps moving left
#           right = mid
#   Returns: 2 (index of first 30, value=30)
#            [10,20,30,30,30,40,50]
#             0  1  2  3  4  5  6
#                   ↑ index 2
#
# bisect_right: Find position AFTER last occurrence
#   while left < right:
#       if arr[mid] > target:   # > allows equal values
#           right = mid
#   Returns: 5 (index after last 30, value=40)
#            [10,20,30,30,30,40,50]
#             0  1  2  3  4  5  6
#                            ↑ index 5
#
# Key difference: >= vs > in the condition
# This problem uses bisect_left pattern to find first bad version
```

## Complexity

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

## Tags

[Grind 75](/catalog/grind-75), [Grind](/catalog/grind).
