Search⌘ K
AI Features

First Bad Version

Understand how to identify the first bad version in a sequence of software releases using modified binary search. This lesson guides you through minimizing API checks to quickly pinpoint the critical version causing failures, enhancing problem-solving skills for coding interviews.

Statement

You are managing a product development team, and the latest release has failed quality checks. Because each version is built on top of the previous one, once a version is bad, every version after it is also bad.

You are given an array of n versions [1,2,,n][1, 2, …, n], and your task is to determine the first version in this sequence that is bad—the version that causes all later versions to be bad as well.

You have access to an API isBadVersion(version) that returns TRUE if a given version is bad.

Your task is to find the first bad version while minimizing the number of calls to this API.

Constraints:

  • 11 \leq bad \leq n 105\leq 10^5

Examples

canvasAnimation-image
1 / 3

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

First Bad Version

1.

Given n = 1010, if version 77 is the first bad version, how many API calls are required to find it?

A.

11

B.

22

C.

33

D.

44


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6

Try it yourself

Implement your solution in the following coding playground.

Python
usercode > main.py
''' The self.is_bad_version(version) API is already defined for you
which returns TRUE if the current version is bad.
'''
from bad_version import BadVersion
class Solution(BadVersion):
def __init__(self, bad):
super().__init__(bad)
def first_bad_version(self, n):
# Replace this placeholder return statement with your code
return 0
First Bad Version