Home
DSA Patterns
Binary Search
First Bad Version
First Bad Version (LeetCode 278)
Find the first bad version with the fewest isBadVersion calls by binary searching for the first true in a false-then-true sequence.
Problem Understanding
First Bad Version: you have versions 1..n. Once a version is bad, every later version is bad too. Using the API isBadVersion(version), find the first bad version while making as few calls as possible.
Example: n = 5, first bad = 4: isBadVersion(3) is false, isBadVersion(4) is true, so the answer is 4.
The Intuition: Find the First True
Line the versions up and write each one's verdict: good, good, good, bad, bad. That is a sorted sequence of booleans, so binary search finds the boundary in O(log n) calls:
- Check
mid. - Bad: the first bad version is
midor earlier. Keepmid:hi = mid. - Good: the first bad version is after
mid:lo = mid + 1. - When
lo == hi, you are standing on it.
This "first index where a condition becomes true" template also solves Search Insert Position and Sqrt(x) from the other side.
First Bad Version Visualization
SEARCH [1, n]
versions (✓ good / ✗ bad once checked)
lo
hi
calls
0
range
1..5
Answer
:
The first bad version is somewhere in [1, 5]
Next
isBadVersion(3) = false: everything up to 3 is good. lo = 4
n
bad
Each version is a cell. The search cannot see which ones are bad, so a verdict appears only once a version is checked: ✓ for good, ✗ for bad. The range [lo, hi] is tinted. Try First is bad and Last is bad: both take only 4 calls for 16 versions.
First Bad Version Dry Run
Input: n = 5, first bad = 4
| lo | hi | mid | isBadVersion(mid) | Move |
|---|---|---|---|---|
| 1 | 5 | 3 | false | lo = 4 |
| 4 | 5 | 4 | true | hi = 4 |
lo = hi = 4. Answer: 4, found with 2 calls.
First Bad Version LeetCode Solution
Find-first-true binary search. isBadVersion is provided by LeetCode in every language.
First Bad Version Code
1var solution = function(isBadVersion) {2 return function(n) {3 let lo = 1, hi = n;4 while (lo < hi) {5 const mid = lo + Math.floor((hi - lo) / 2);6 if (isBadVersion(mid)) hi = mid;7 else lo = mid + 1;8 }9 return lo;10 };11};
First Bad Version in Python, Java, C++, TypeScript and C
First Bad Version in Python
class Solution:
def firstBadVersion(self, n: int) -> int:
lo, hi = 1, n
while lo < hi:
mid = lo + (hi - lo) // 2
if isBadVersion(mid):
hi = mid
else:
lo = mid + 1
return loFirst Bad Version in Java
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int lo = 1, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (isBadVersion(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}
}First Bad Version in C++
class Solution {
public:
int firstBadVersion(int n) {
int lo = 1, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (isBadVersion(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}
};First Bad Version in TypeScript
var solution = function(isBadVersion: (v: number) => boolean) {
return function(n: number): number {
let lo = 1, hi = n;
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (isBadVersion(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
};
};First Bad Version in C
int firstBadVersion(int n) {
int lo = 1, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (isBadVersion(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}Edge Cases & Common Mistakes
- n = 1: the only version must be the bad one; the loop does not run.
- Overflow:
(lo + hi) / 2overflows for n near 2³¹ − 1; uselo + (hi − lo) / 2. hi = mid − 1on a bad mid: can skip the answer when mid is the first bad version.while (lo <= hi)withhi = mid: loops forever once lo equals hi; uselo < hi.- Scanning from version 1: correct but O(n) API calls; the problem asks for the minimum.
Summary
isBadVersion goes false, then true, so binary search for the first true: a bad mid stays in the range (hi = mid), a good mid is skipped (lo = mid + 1). Compute mid as lo + (hi − lo) / 2 to avoid overflow. O(log n) calls and O(1) space.
More practice: DSA interview questions with answers · patterns cheat sheet
