First Flagged Build in a Deployment Pipeline
Heaps, Sorting & SearchAuto-graded · 18 test casesLast updated
Tests whether a candidate can recognize a monotonic predicate and apply binary search to find the first transition point without comparing version strings.
Problem
Your team runs an automated deployment pipeline that releases software builds in chronological order. Each build has a version label such as "2.3.1" or "10.0.4" following the MAJOR.MINOR.PATCH format.
An external health-check system has already evaluated every build and recorded the results in a boolean list called flagged. A True entry means that build introduced a critical regression; a False entry means it passed. The pipeline guarantees a clean-then-broken structure: once a build is flagged, every later build is also flagged.
Given the ordered list of version labels and the corresponding flag results, return the version label of the first flagged build. If no build is flagged, return an empty string "".
Constraints
1 <= len(versions) == len(flagged) <= 10^5- Each element of
versionsis a non-empty string inMAJOR.MINOR.PATCHformat where each component is a non-negative integer. flaggedis a list of booleans that forms a (possibly empty) prefix ofFalsevalues followed by a (possibly empty) suffix ofTruevalues.- The two lists have the same length.
- Minimize the number of entries in
flaggedyou inspect.
Examples
Example 1
Input: first_flagged_build(['1.0.0', '1.0.1', '1.1.0', '1.2.0'], [False, False, True, True])
Output: '1.1.0'
Example 2
Input: first_flagged_build(['1.0.0', '1.0.1'], [False, False])
Output: ''
Hints
Hint 1
The version strings are not what you search over — you are searching over indices. Ask yourself: what property of the index array makes binary search applicable?
Hint 2
Think about a half-open window [lo, hi). What invariant can you maintain about what is known to be flagged versus not flagged at every step?
Hint 3
When you probe the midpoint and it is flagged, the answer could be at mid or earlier — shrink from the right. When it is not flagged, the answer must be strictly to the right — shrink from the left. When lo == hi you are done.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The version strings are a red herring. You never need to parse or compare them. What matters is the structure of flagged: it is a boolean list that looks like some number of False values followed by some number of True values — a classic monotonic predicate. The task is to find the index of the leftmost True, which is exactly a lower-bound binary search.
Loop invariant
Maintain a half-open window [lo, hi) with this invariant at the start of every iteration:
- Every index
< lois known to be clean (False). - Every index
>= hiis known to be flagged (True), or is past the end of the list.
The answer — the first flagged index — therefore lies in [lo, hi).
Initialization: lo = 0, hi = len(versions). No indices exist below 0, and no indices inside the list exist at or above n, so both halves of the invariant hold vacuously.
Step: Probe mid = (lo + hi) // 2, which always satisfies lo <= mid < hi.
- If
flagged[mid]isTrue: by monotonicity, everything frommidonward is also flagged. The answer is atmidor to its left, so sethi = mid. - If
flagged[mid]isFalse: by monotonicity, everything up to and includingmidis clean. The answer is strictly to the right, so setlo = mid + 1.
Either way the window shrinks by at least one, guaranteeing termination.
Result: When lo == hi, the invariant says everything before lo is clean and everything from lo onward is flagged (or the list ended). So lo is exactly the first flagged index. If lo == len(versions), no build was flagged and we return "".
Why hi = n is the right initialization
Starting hi at n rather than n - 1 means the "nothing is flagged" case resolves naturally: lo simply grows to n and the final check lo < len(versions) catches it. No extra branch or post-loop probe is needed, and there is no off-by-one risk.
Edge cases
| Situation | Outcome |
|---|---|
| Single build, clean | One probe, lo reaches 1 == n, returns "" |
| Single build, flagged | One probe, hi drops to 0, returns versions[0] |
| All builds flagged | hi shrinks to 0 on the first probe, returns versions[0] |
| No builds flagged | lo grows to n, returns "" |
Version labels like "1.10.0" after "1.2.0" | Irrelevant; search is over indices only |
Complexity
- Time: O(log n) — the window halves each iteration.
- Entries of
flaggedinspected: at most ⌈log₂(n + 1)⌉, which is 17 for n = 10⁵. A linear scan could require up to 100,000 lookups. - Space: O(1) extra.
This call count is optimal: there are n + 1 possible answers, and each yes/no probe at best halves the remaining possibilities.
from typing import List
def first_flagged_build(versions: List[str], flagged: List[bool]) -> str:
lo, hi = 0, len(versions) # half-open search window [lo, hi)
while lo < hi:
mid = (lo + hi) // 2
if flagged[mid]:
hi = mid # mid is flagged; answer is at mid or earlier
else:
lo = mid + 1 # mid is clean; answer is strictly to the right
return versions[lo] if lo < len(versions) else ""