1520. Broken Stretch of Builds

Medium3.5 points estimatedBinary SearchInteractive

A build server numbers its releases 1 to n. From some release firstBad onward every release is bad, and every earlier release is good. You are not told firstBad, but known is a release guaranteed to be good, so 1 <= known < firstBad <= n. You test a release by calling the global function isBadVersion(v), which returns true when release v is bad (api is the handle to the same service).

Return the array [firstBad, count], where count is the number of bad releases, that is n - firstBad + 1. Search only the range after known, use midpoints that cannot overflow a 32-bit integer, and call isBadVersion at most 64 times. Target O(log n) time and O(1) extra space.

Example 1

Input:
n = 10, known = 3, api = 7
Output:
[7,4]
Explanation:

Releases 7 through 10 are bad, so the first bad one is 7 and there are 4 bad releases.

Example 2

Input:
n = 2, known = 1, api = 2
Output:
[2,1]
Explanation:

Only release 2 exists beyond the known good release 1, and it is bad, so the answer is [2, 1].

Example 3

Input:
n = 100, known = 40, api = 41
Output:
[41,60]
Explanation:

The first bad release is 41, leaving releases 41 through 100, which is 60 bad releases.

Constraints

2 ≤ n ≤ 231 - 1; 1 ≤ known < firstBad ≤ n; at most 64 calls to isBadVersion; overflow-safe midpoints

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Calls
You may call isBadVersion(version) at most 64 times per case. It is provided; you do not write it.

Expected complexity

Time
O(log n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…