Drill

ProblemsJava › patterns

Which commit broke the build

mediumpatternsBinary searchArraysJava

Every commit up to some point built cleanly and every one after it fails. A bisect finds the first bad one without building them all.

firstBadBuild(passed: list<bool>) → int

Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

int firstBadBuild(List<Boolean> passed) {
    
}

Worked examples

CallResult
firstBadBuild(Main.<Boolean>ls(true, true, false, false))2
firstBadBuild(Main.<Boolean>ls(true, true, true))-1
firstBadBuild(Main.<Boolean>ls(false, false))0
firstBadBuild(Main.<Boolean>ls())-1

Hint

This is what `git bisect` does. Halve the range, and let a failure pull the right edge in while a pass pushes the left edge out.

Reference solution in Java
int firstBadBuild(List<Boolean> passed) {
    int lo = 0, hi = passed.size();
    while (lo < hi) {
        int mid = (lo + hi) / 2;
        if (passed.get(mid)) lo = mid + 1;
        else hi = mid;
    }
    return lo == passed.size() ? -1 : lo;
}

The same problem in another language

More patterns problems in Java