Skip to contentKEEP THE CRAFTTHE SANCTUARY

Find the First True Boundary

Binary search for the first True: if mid is True, save it and keep looking to the left.

The problem

Implement find_first_true(flags). flags holds zero or more False values followed by zero or more True values. Return the index of the first True, or len(flags) if there is no True. For example, [False, False, True, True] gives 2 and [False, False] gives 2. Aim for O(log n) time.

Starter code

def find_first_true(flags):
    pass

Public examples

  1. flags = [False, False, True, True]gives2
  2. flags = [True, True]gives0

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.