KatInterview

Locate the Transition Point in a Monotonic Boolean Array

Heaps, Sorting & SearchAuto-graded · 18 test cases
MediumAuto-gradedHeaps, Sorting & Searchbinary-searcharraysboundaries

Last 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 False appears after any True
  • Your solution must handle large inputs efficiently

Notes

  • Use zero-based indexing
  • The array may be empty
  • The array may contain only False values
  • The array may contain only True values

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?