Bisect a version history with flaky checks, and answer what a value was at a past time by searching timestamps.
Every version before lo is good and every version from hi on is bad or does not exist, so lo ends as the first bad version.
Use it when the versions are in order, a bad version stays bad, and you can test one version. Signal words: the first bad version, the first time a limit was passed, bisect.
Start: lo is 0. hi is the number of versions, or a version known to be bad.
Step: while lo < hi, set mid = lo + (hi - lo) / 2 and test version mid. If it is bad, set hi = mid. Otherwise set lo = mid + 1.
Stop: when lo == hi. The answer is lo. If lo is the number of versions, no version is bad.
It does not work when a bad version can become good again, or when a test result can be wrong in both directions.
Cost: log2 of the number of versions, plus 1, tests, each costing its repeated runs.
def first_bad(runs):
# [1] The window [lo, hi) holds the versions in question. hi = n stands for no bad version, and counts as bad.
lo = 0
hi = len(runs)
# [2] While the window holds a version, look at the version in its middle.
while lo < hi:
mid = (lo + hi) // 2
# [3] The check: run the version up to its repeats, and stop at the first failed run. One failed run is proof of a bad version.
failed = False
for outcome in runs[mid]:
if outcome == 1:
failed = True
break
if failed:
# [4] A bad version: so is every later version. It may be the first, so hi moves to it.
hi = mid
else:
# [5] A good version: so is every earlier version. lo moves past it.
lo = mid + 1
# [6] The window is empty: lo is the first bad version, or n when there is none.
return lopublic int firstBad(int[][] runs) {
// [1] The window [lo, hi) holds the versions in question. hi = n stands for no bad version, and counts as bad.
int lo = 0;
int hi = runs.length;
// [2] While the window holds a version, look at the version in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
// [3] The check: run the version up to its repeats, and stop at the first failed run. One failed run is proof of a bad version.
boolean failed = false;
for (int outcome : runs[mid]) {
if (outcome == 1) {
failed = true;
break;
}
}
if (failed) {
// [4] A bad version: so is every later version. It may be the first, so hi moves to it.
hi = mid;
} else {
// [5] A good version: so is every earlier version. lo moves past it.
lo = mid + 1;
}
}
// [6] The window is empty: lo is the first bad version, or n when there is none.
return lo;
}int firstBad(vector<vector<int>>& runs) {
// [1] The window [lo, hi) holds the versions in question. hi = n stands for no bad version, and counts as bad.
int lo = 0;
int hi = (int)runs.size();
// [2] While the window holds a version, look at the version in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
// [3] The check: run the version up to its repeats, and stop at the first failed run. One failed run is proof of a bad version.
bool failed = false;
for (int outcome : runs[mid]) {
if (outcome == 1) {
failed = true;
break;
}
}
if (failed) {
// [4] A bad version: so is every later version. It may be the first, so hi moves to it.
hi = mid;
} else {
// [5] A good version: so is every earlier version. lo moves past it.
lo = mid + 1;
}
}
// [6] The window is empty: lo is the first bad version, or n when there is none.
return lo;
}function firstBad(runs) {
// [1] The window [lo, hi) holds the versions in question. hi = n stands for no bad version, and counts as bad.
let lo = 0;
let hi = runs.length;
// [2] While the window holds a version, look at the version in its middle.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
// [3] The check: run the version up to its repeats, and stop at the first failed run. One failed run is proof of a bad version.
let failed = false;
for (const outcome of runs[mid]) {
if (outcome === 1) {
failed = true;
break;
}
}
if (failed) {
// [4] A bad version: so is every later version. It may be the first, so hi moves to it.
hi = mid;
} else {
// [5] A good version: so is every earlier version. lo moves past it.
lo = mid + 1;
}
}
// [6] The window is empty: lo is the first bad version, or n when there is none.
return lo;
}def value_at(times, values, t):
# [1] The window [lo, hi) holds the changes in question. hi = n stands for no change after the time.
lo = 0
hi = len(times)
# [2] While the window holds a change, look at the change in its middle.
while lo < hi:
mid = (lo + hi) // 2
if times[mid] <= t:
# [3] The change is at or before the time, and so is every earlier change. lo moves past it.
lo = mid + 1
else:
# [4] The change is after the time, and so is every later change. It may be the first, so hi moves to it.
hi = mid
# [5] lo is the first change after the time. The change before it was in force. With no such change, the answer is -1.
return values[lo - 1] if lo > 0 else -1public int valueAt(int[] times, int[] values, long t) {
// [1] The window [lo, hi) holds the changes in question. hi = n stands for no change after the time.
int lo = 0;
int hi = times.length;
// [2] While the window holds a change, look at the change in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (times[mid] <= t) {
// [3] The change is at or before the time, and so is every earlier change. lo moves past it.
lo = mid + 1;
} else {
// [4] The change is after the time, and so is every later change. It may be the first, so hi moves to it.
hi = mid;
}
}
// [5] lo is the first change after the time. The change before it was in force. With no such change, the answer is -1.
return lo > 0 ? values[lo - 1] : -1;
}int valueAt(vector<int>& times, vector<int>& values, long long t) {
// [1] The window [lo, hi) holds the changes in question. hi = n stands for no change after the time.
int lo = 0;
int hi = (int)times.size();
// [2] While the window holds a change, look at the change in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (times[mid] <= t) {
// [3] The change is at or before the time, and so is every earlier change. lo moves past it.
lo = mid + 1;
} else {
// [4] The change is after the time, and so is every later change. It may be the first, so hi moves to it.
hi = mid;
}
}
// [5] lo is the first change after the time. The change before it was in force. With no such change, the answer is -1.
return lo > 0 ? values[lo - 1] : -1;
}function valueAt(times, values, t) {
// [1] The window [lo, hi) holds the changes in question. hi = n stands for no change after the time.
let lo = 0;
let hi = times.length;
// [2] While the window holds a change, look at the change in its middle.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (times[mid] <= t) {
// [3] The change is at or before the time, and so is every earlier change. lo moves past it.
lo = mid + 1;
} else {
// [4] The change is after the time, and so is every later change. It may be the first, so hi moves to it.
hi = mid;
}
}
// [5] lo is the first change after the time. The change before it was in force. With no such change, the answer is -1.
return lo > 0 ? values[lo - 1] : -1;
}This version tests each version once, with its first run. A bad version that passes its first run looks good.
This version calls a version bad when more than half of its runs failed. A bad version that fails one run in three looks good.
This version of the time lookup moves lo only when times[mid] < t. A change made exactly at the time is then treated as later.
This version returns values[lo - 1] without checking that lo is above 0. In Python, position -1 is the last entry.
Suppose a bug is fixed in version 3, so the versions read good, bad, bad, good, good, good, good, good. The check is false, true, true, false: it does not flip once.
systemdesignmastery.dev/dsa/cheatsheets/first-bad-deploy