beginner · binary search · monotonic predicate · search on answer
Find the first broken build
Problem
Builds are numbered 1 to n. Once a build is broken, every later build is broken too, and build n is known to be broken. Checking a build is slow. Find the first broken build with as few checks as possible.
Constraints
- 1 ≤ n ≤ 32 (visualization limit; the method works for any n)
- a check isBroken(v) is the only way to learn about build v
Examples
n = 8, builds 5…8 are broken → 5
Show the solution
Binary search on a monotonic predicate
The predicate splits the builds into a working prefix and a broken suffix; each check halves the range that can contain the boundary.
Why it is correct
Invariant: the first broken build is in [lo, hi]. A broken mid keeps the answer in [lo, mid]; a working mid moves it to [mid + 1, hi]. When lo = hi, that is the answer.
Complexity
Time O(log n) checks, extra space O(1). Each check halves the range: ⌈log₂ n⌉ checks.
For contrast: Check builds in order. That costs O(n) checks time and O(1) space.
Edge cases
- Build 1 already broken.
- Only build n broken.
- n = 1: no check needed.
Common mistakes
- hi = mid − 1 after a broken mid, which can skip the answer.
- Computing (lo + hi) / 2 in fixed-width integers for huge n.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class FirstBrokenBuild
{
/// <summary>The first v in 1..n with isBroken(v), given that broken builds form a suffix and build n is broken. O(log n) checks.</summary>
public static int Find(int n, Func<int, bool> isBroken)
{
int lo = 1, hi = n; // the answer is in [lo, hi]
while (lo < hi)
{
int mid = lo + (hi - lo) / 2;
if (isBroken(mid))
hi = mid; // mid or earlier
else
lo = mid + 1; // strictly later
}
return lo;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/FirstBrokenBuild.cs
from collections.abc import Callable
def find(n: int, is_broken: Callable[[int], bool]) -> int:
"""The first v in 1..n with is_broken(v), given that broken builds form a suffix and build n is broken. O(log n) checks."""
lo, hi = 1, n # the answer is in [lo, hi]
while lo < hi:
mid = (lo + hi) // 2
if is_broken(mid):
hi = mid # mid or earlier
else:
lo = mid + 1 # strictly later
return lo
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/first_broken_build.py
package dev.bekadan.dsa.problems;
import java.util.function.IntPredicate;
public final class FirstBrokenBuild {
private FirstBrokenBuild() {
}
/** The first v in 1..n with isBroken(v), given that broken builds form a suffix and build n is broken. O(log n) checks. */
public static int find(int n, IntPredicate isBroken) {
int lo = 1, hi = n; // the answer is in [lo, hi]
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (isBroken.test(mid)) {
hi = mid; // mid or earlier
} else {
lo = mid + 1; // strictly later
}
}
return lo;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/FirstBrokenBuild.java
#pragma once
namespace bekadan::dsa::problems {
// The first v in 1..n with is_broken(v), given that broken builds form a suffix and build n is broken. O(log n) checks.
template <typename Predicate>
int first_broken_build(int n, Predicate is_broken) {
int lo = 1, hi = n; // the answer is in [lo, hi]
while (lo < hi) {
const int mid = lo + (hi - lo) / 2;
if (is_broken(mid))
hi = mid; // mid or earlier
else
lo = mid + 1; // strictly later
}
return lo;
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/first_broken_build.hpp