FastPrepFirst Bad Version in a Monotone Array

First Bad Version in a Monotone Array

Meta logoMetaEasyFULLTIMEPHONE SCREEN
Learn

Problem statement

The Boolean array bad describes software versions in chronological order. Once a version is bad, every later version is also bad.

Return the zero-based index of the first bad version, or -1 when every version is good.

Function

firstBadVersion(bad: boolean[]) → int

Examples

Example 1

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

Index 2 is the first true entry.

Example 2

bad = [true,true]return = 0

The first version is already bad.

Example 3

bad = [false,false]return = -1

No bad version exists.

Constraints

  • 1 <= bad.length <= 10^6.
  • The array is monotone: no false appears after a true.

More Meta problems

See Meta hiring insights
public int firstBadVersion(boolean[] bad) {
    // Write your solution here.
}
bad[false,false,true,true]
expected2
Checking account…