Skip to main content

First Bad Version - Solution & Explanation

EasyBinary SearchInteractive17 min readAsked at: Amazon, Microsoft, Apple +4
Practice this problem

Problem Statement

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 - 1

Approach Overview

Problem 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 1: Binary Search Optimization

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(log n)
Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Recursive Binary Search Method

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(log n)
Space Complexity: O(log n) - due to recursion stack

Try this approach in the editor →

Approach 3: Binary Search

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

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Binary Search Optimization

Time Complexity: O(log n)
Space Complexity: O(1)

Recursive Binary Search Method

Time Complexity: O(log n)
Space Complexity: O(log n) - due to recursion stack

Binary Search—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Linear ScanO(n)O(1)Simple baseline solution or when n is very small
Binary Search OptimizationO(log n)O(1)Optimal approach when versions follow a monotonic good→bad pattern
Recursive Binary SearchO(log n)O(log n)When recursion is preferred for divide-and-conquer clarity

Video Solution

First bad version | Leetcode #278 • Techdose • 55,042 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is First Bad Version easy or hard?
First Bad Version is categorized as an Easy problem on LeetCode. The implementation is short, but it tests whether you recognize the monotonic pattern and apply binary search correctly to minimize API calls.
First Bad Version Python/Java solution
Both Python and Java implementations follow the same binary search pattern. Maintain left and right boundaries, check the middle version using isBadVersion, and shrink the search space accordingly. The final index where left equals right is the first bad version with O(log n) time complexity.
How to solve First Bad Version in O(log n)?
Use binary search on the version range from 1 to n. Compute mid and call isBadVersion(mid). If mid is bad, move the right boundary to mid; otherwise move the left boundary to mid + 1. Continue until both pointers meet, which identifies the first bad version in O(log n) time.
What is the best approach for First Bad Version?
Binary search is the optimal approach because versions form a monotonic sequence: all good versions come before bad ones. By repeatedly halving the search range, you find the first bad version in O(log n) time while using only O(1) space. This also minimizes expensive API calls to isBadVersion.
Is First Bad Version asked at Google/Amazon/Meta?
First Bad Version is a classic binary search interview question and has appeared in coding interviews at companies like Google and Amazon. It tests recognition of monotonic conditions and efficient search using minimal API calls.
What data structure is used in First Bad Version?
No special data structure is required. The solution relies on the binary search algorithm over an implicit sorted range of version numbers. The monotonic property (good versions followed by bad versions) enables efficient searching.
What is the time complexity of First Bad Version?
The optimal binary search solution runs in O(log n) time because the search range is halved on every step. A naive linear scan would take O(n) time since it may check every version sequentially. Space complexity is O(1) for the iterative solution.

Ready to solve this problem?

Practice First Bad Version with our built-in code editor and test cases.

Practice on FleetCode