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.
Example 1:
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.
Example 2:
Input: n = 1, bad = 1 Output: 1
Constraints:
1 <= bad <= n <= 231 - 1Problem Overview: You receive n product versions and an API isBadVersion(version). Once a version becomes bad, every version after it is also bad. The goal is to find the first bad version while minimizing API calls.
Approach 1: Linear Scan (O(n) time, O(1) space)
The most straightforward strategy is checking each version sequentially from 1 to n. Call isBadVersion(i) for every version until the API returns true. The first time it returns true is the answer. This works because versions before the first bad one are guaranteed to be good. The downside is efficiency: in the worst case you call the API n times, which becomes expensive for very large inputs.
Approach 2: Binary Search Optimization (O(log n) time, O(1) space)
The key observation is that the versions form a monotonic pattern: a sequence of good versions followed by bad ones. That structure makes the problem ideal for binary search. Start with a search range [1, n]. Check the middle version. If isBadVersion(mid) is true, the first bad version lies in the left half (including mid). Otherwise, it must be in the right half. Narrow the search range until left == right. That index is the first bad version. This approach drastically reduces API calls from O(n) to O(log n).
Approach 3: Recursive Binary Search Method (O(log n) time, O(log n) space)
This version applies the same binary search idea but uses recursion instead of a loop. Each recursive call splits the search range and evaluates isBadVersion(mid). If the mid version is bad, recurse into the left half; otherwise recurse into the right half. The logic mirrors divide-and-conquer patterns common in binary search and interactive API problems. The time complexity remains O(log n), but recursion adds a call stack cost of O(log n).
Recommended for interviews: Interviewers expect the binary search solution. The linear scan proves you understand the problem constraints, but it fails scalability requirements. Implementing the iterative binary search shows you recognize the monotonic property and can minimize expensive API calls—exactly the optimization interviewers look for in search problems.
This approach uses the concept of binary search to efficiently find the first bad version by minimizing the number of calls made to the given isBadVersion API. By leveraging binary search, we can reduce the problem set size by half with each iteration, therefore achieving O(log n) time complexity.
The code above implements the binary search algorithm. It initializes two pointers, left and right, to perform the search. If isBadVersion(mid) is true, the first bad version must be at mid or to its left, hence right is adjusted. Otherwise, the bad version must be to the right of mid, hence left is adjusted. The loop continues until left meets right, at which point the first bad version is identified.
Time Complexity: O(log n)
Space Complexity: O(1)
This approach explores a recursive implementation of the binary search technique to identify the first bad version. Employing recursion allows the continual division of the search problem into smaller subproblems until the solution is found.
The solution leverages a recursive helper function, binarySearch, to apply the binary search paradigm. isBadVersion checks are made mid way through the current range of versions. The range is recursively narrowed down based on this evaluation until left identifies the first bad version.
Time Complexity: O(log n)
Space Complexity: O(log n) - due to recursion stack
We define the left boundary of the binary search as l = 1 and the right boundary as r = n.
While l < r, we calculate the middle position mid = \left\lfloor \frac{l + r}{2} \right\rfloor, then call the isBadVersion(mid) API. If it returns true, it means the first bad version is between [l, mid], so we set r = mid; otherwise, the first bad version is between [mid + 1, r], so we set l = mid + 1.
Finally, we return l.
The time complexity is O(log n), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
Rust
JavaScript
| Approach | Complexity |
|---|---|
| Binary Search Optimization | Time Complexity: |
| Recursive Binary Search Method | Time Complexity: |
| Binary Search | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Linear Scan | O(n) | O(1) | Simple baseline solution or when n is very small |
| Binary Search Optimization | O(log n) | O(1) | Optimal approach when versions follow a monotonic good→bad pattern |
| Recursive Binary Search | O(log n) | O(log n) | When recursion is preferred for divide-and-conquer clarity |
First bad version | Leetcode #278 • Techdose • 53,844 views views
Watch 9 more video solutions →Practice First Bad Version with our built-in code editor and test cases.
Practice on FleetCode