Drill

ProblemsC# › patterns

Which commit broke the build

mediumpatternsBinary searchArraysC#

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

C# 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

public int FirstBadBuild(List<bool> passed) {
    
}

Worked examples

CallResult
FirstBadBuild(new List<bool> { true, true, false, false })2
FirstBadBuild(new List<bool> { true, true, true })-1
FirstBadBuild(new List<bool> { false, false })0
FirstBadBuild(new List<bool> { })-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 C#
public int FirstBadBuild(List<bool> passed) {
    int lo = 0, hi = passed.Count;
    while (lo < hi) {
        int mid = (lo + hi) / 2;
        if (passed[mid]) lo = mid + 1;
        else hi = mid;
    }
    return lo == passed.Count ? -1 : lo;
}

The same problem in another language

More patterns problems in C#