Find the Earliest Failing Version
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
Overview: Find the first failing version using only a monotone pass-or-fail API while minimizing queries.
Find the first failing version using only a monotone pass-or-fail API while minimizing queries.
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
Overview: Find the first failing version using only a monotone pass-or-fail API while minimizing queries.
Versions are numbered from 1 through n. There is a first failing version: every earlier version passes, and that version and every later version fail.
Implement Solution.firstBadVersion(n) to return the smallest failing version number. Use the provided isBadVersion(version) API to inspect a version, and minimize the number of API calls. The latest version is guaranteed to fail.
isBadVersion(version) returns a Boolean: True for a failing version and False for a passing one. Query only version numbers in 1..n.
For this Python practice interface, construct Solution(isBadVersion) with the supplied callable, then call firstBadVersion(n). The constructor injects the API; it does not reveal its hidden boundary. The candidate must not inspect the callable's owner, attributes, closure or source. The first failing version shown in an example is private setup data, not a parameter to firstBadVersion.
Example 1: There are 12 versions, and the private test service's first failing version is 9. Calling firstBadVersion(12) returns 9: versions 1..8 pass and 9..12 fail.
Example 2: There are 7 versions, and every version fails. Calling firstBadVersion(7) returns 1.
1 <= first failing version <= n <= 2**31 - 1
.