278. First Bad Version
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the first bad version from a sequence of n versions, numbered 1 to n. We are given that if a version is bad, all subsequent versions are also bad. This creates a distinct boundary between good and bad versions. The versions can be represented as a sequence of states like [Good, Good, ..., Good, Bad, Bad, ..., Bad]. If we use the provided API isBadVersion(version), this corresponds to a boolean array [false, false, ..., false, true, true, ..., true].
Solution
First Bad Version in Python
# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:
class Solution:
def firstBadVersion(self, n: int) -> int:
left, right = 1, n
# Perform binary search. The search space is [left, right].
# We are looking for the first version `v` where isBadVersion(v) is True.
while left < right:
# Python integers have arbitrary precision, but this formula is a good habit.
mid = left + (right - left) // 2
if isBadVersion(mid):
# If mid is bad, it could be the first bad version.
# The answer must be in the range [left, mid].
right = mid
else:
# If mid is good, the first bad version must be after mid.
# The answer must be in the range [mid + 1, right].
left = mid + 1
# When the loop terminates, left == right, which is the first bad version.
return left
First Bad Version in Java
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int left = 1;
int right = n;
// Binary search to find the first occurrence of a bad version.
// The loop invariant is that the first bad version is always in the range [left, right].
while (left < right) {
// Prevent potential integer overflow for mid calculation.
int mid = left + (right - left) / 2;
if (isBadVersion(mid)) {
// If mid is bad, the first bad version is at 'mid' or to its left.
// So, we shrink the search space to [left, mid].
right = mid;
} else {
// If mid is good, the first bad version must be to its right.
// So, we shrink the search space to [mid + 1, right].
left = mid + 1;
}
}
// The loop terminates when left == right. This is the first bad version.
return left;
}
}
First Bad Version in C++
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int left = 1;
int right = n;
// Use binary search to find the first bad version.
// The search space is [left, right].
while (left < right) {
// Calculate mid point to avoid potential overflow.
int mid = left + (right - left) / 2;
if (isBadVersion(mid)) {
// If mid is a bad version, it could be the first one.
// The first bad version is in the range [left, mid].
right = mid;
} else {
// If mid is a good version, the first bad version must be after mid.
// The first bad version is in the range [mid + 1, right].
left = mid + 1;
}
}
// When the loop terminates, left == right, and it points to the first bad version.
return left;
}
};
First Bad Version in TypeScript
/**
* The knows API is defined in the parent class Relation.
* isBadVersion(version: number): boolean {
* ...
* };
*/
var solution = function(isBadVersion: any) {
/**
* @param {number} n Total versions
* @return {number} The first bad version
*/
return function(n: number): number {
let left = 1;
let right = n;
// Binary search to find the boundary between good and bad versions.
while (left < right) {
// Use Math.floor to ensure mid is an integer.
// This formula also prevents potential overflow with large numbers.
let mid = Math.floor(left + (right - left) / 2);
if (isBadVersion(mid)) {
// If mid is bad, it might be the first bad one.
// We search in the left half, including mid.
right = mid;
} else {
// If mid is good, the first bad one must be to its right.
// We search in the right half.
left = mid + 1;
}
}
// The loop ends when left and right converge to the same value,
// which is the first bad version.
return left;
};
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.