Locate the Transition Point in a Monotonic Boolean Array
Heaps, Sorting & SearchAuto-graded · 18 test casesLast updated
Identify the earliest index where a boolean array transitions from False to True, testing binary search on boundaries and edge cases.
Locate the Transition Point in a Monotonic Boolean Array
You receive a boolean array versions where each element indicates whether a corresponding item meets a certain condition. The array is monotonic: once a True value appears, all subsequent values are also True. In other words, the array consists of zero or more False values followed by zero or more True values.
Your task is to identify the index of the first True value. If no True value exists in the array, return -1.
Function Signature
def find_transition_point(versions: list[bool]) -> int:
...
Constraints
0 <= len(versions) <= 1_000_000- The array is monotonic: no
Falseappears after anyTrue - Your solution must handle large inputs efficiently
Notes
- Use zero-based indexing
- The array may be empty
- The array may contain only
Falsevalues - The array may contain only
Truevalues
Examples
Example 1
Input: find_transition_point([False, False, True, True, True])
Output: 2
Example 2
Input: find_transition_point([False, False])
Output: -1
Example 3
Input: find_transition_point([True])
Output: 0
Hints
Hint 1
The monotonic property means the array is partitioned into two segments—exploit this structure rather than checking every element.
Hint 2
Use binary search to find the boundary between False and True values. Think about what invariant you need to maintain about the search bounds.
Hint 3
Handle edge cases carefully: what should happen when the array is empty, or when all values are False, or when the first element is True?
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The array has a special structure: all False values come before all True values. This monotonic property makes it perfect for binary search. We're not searching for a specific value—we're searching for the transition point where the array changes from False to True.
Use a half-open interval [lo, hi) where lo starts at 0 and hi starts at len(versions). Think of hi = len(versions) as representing "no transition exists." At each iteration, examine the middle element:
- If
versions[mid]isTrue, the transition occurs at or beforemid. Movehitomid(keepingmidin the search range since it might be our answer). - If
versions[mid]isFalse, the transition must occur aftermid. Movelotomid + 1.
When lo equals hi, we've found the boundary. If lo < len(versions), it points to the first True. Otherwise, no True exists, so return -1.
Why It Works
We maintain an invariant: all indices before lo contain False, and all indices at or after hi contain True (treating indices beyond the array as virtually True).
- Initially: No indices are before
0, and no real indices are at or beyondlen(versions). The invariant holds. - When
versions[mid]isTrue: By monotonicity, all indices frommidonward areTrue. Settinghi = midpreserves the invariant. - When
versions[mid]isFalse: By monotonicity, all indices up to and includingmidareFalse. Settinglo = mid + 1preserves the invariant. - At termination:
lo == hirepresents the exact transition point. Everything before isFalse, everything from that point on isTrue.
The algorithm never accesses invalid indices. Since mid < hi, and we only read versions[mid], we stay within bounds even when the array is empty (the loop never executes).
Complexity
Time: O(log n). Each iteration halves the search space. For n = 1,000,000, this means roughly 20 comparisons instead of up to a million with linear search.
Space: O(1). Only a constant number of variables are used.
Edge Cases
- Empty array
[]: Loop doesn't run,lo = 0equalslen(versions), return-1 - All False
[False, False]:loadvances tolen(versions), return-1 - All True
[True, True]:hidrops to0, return0 - Single True
[True]:hibecomes0immediately, return0 - Transition at end
[False, False, True]: Binary search narrows to index2, return2
Solution
def find_transition_point(versions: list[bool]) -> int:
# Use half-open interval [lo, hi)
# Invariant: indices < lo are False, indices >= hi are True
lo, hi = 0, len(versions)
while lo < hi:
mid = (lo + hi) // 2
if versions[mid]:
# Transition is at mid or earlier
hi = mid
else:
# Transition is after mid
lo = mid + 1
# lo is the transition point; if it's beyond the array, no True exists
return lo if lo < len(versions) else -1