Problem · Binary Search

First Bad Version

Learn this problem
EasyQualcommFULLTIMEPHONE SCREEN

Problem statement

A product has versions numbered from 1 through n. Once a version is bad, every later version is also bad.

You are given a boolean array isBad of length n, where isBad[i] reports whether version i + 1 is bad. At least one version is bad.

Return the number of the first bad version.

Function

firstBadVersion(isBad: boolean[]) → int

Examples

Example 1

isBad = [false,false,false,true,true]return = 4

Versions 1 through 3 are good, and version 4 is the first bad version.

Example 2

isBad = [true]return = 1

The only version is bad, so it is also the first bad version.

Example 3

isBad = [false,true,true,true]return = 2

Version 1 is good, while version 2 and every later version are bad.

More Qualcomm problems

drafts saved locally
public int firstBadVersion(boolean[] isBad) {
    // write your code here
}
isBad[false,false,false,true,true]
expected4
checking account