Watch 10 video solutions for First Bad Version, a easy level problem involving Binary Search, Interactive. This walkthrough by Techdose has 53,844 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |