Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Binary Search proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Repeatedly halve an ordered or monotone search space using a boundary invariant. Learn the algorithm's preconditions, state transition, correctness argument, Python cost model, and transfer from one focused drill to a full Interview Problem.
Recognize it when
Consider Binary Search when the prompt's constraints and required operations match this shape: Repeatedly halve an ordered or monotone search space using a boundary invariant.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Binary search is not merely “search a sorted array.” It searches a monotone decision space: once a predicate becomes true, it stays true, or comparisons consistently discard one side. Prove that shape before writing the loop.
For half-open [left, right), state that every index before left is known false and the first true boundary, if it exists, lies inside the unresolved interval. Each iteration must preserve that invariant and strictly shrink the interval.
def binary_search_decisions(values, target):
left, right = 0, len(values) - 1
decisions = []
while left <= right:
mid = (left + right) // 2
decisions.append(mid)
if values[mid] == target:
break
if values[mid] < target:
left = mid + 1
else:
right = mid - 1
return decisionsImplement binary_search_decisions(values, target). values is sorted. Return the indexes inspected by a standard inclusive binary search, stopping when target is found or the interval is empty.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Exact search can stop on equality and often uses an inclusive right edge. Boundary search continues after finding a true candidate, retaining it while searching left. Pick one template deliberately; mixing their update rules creates skipped candidates or infinite loops.
def find_first_true(flags):
left, right = 0, len(flags)
while left < right:
mid = (left + right) // 2
if flags[mid]:
right = mid
else:
left = mid + 1
return leftImplement find_first_true(flags). flags contains zero or more False values followed by zero or more True values. Return the index of the first True value, or len(flags) when no True value exists.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Use binary search when order or a monotone answer space already exists and O(log n) queries are sufficient. Use a hash lookup for average O(1) membership when ordering is irrelevant and extra space is acceptable. Sorting solely for one lookup usually costs more than a scan.
Say: “The predicate is monotone. My half-open interval contains every possible boundary; false discards through mid, true retains mid. The interval shrinks every iteration.” Then cover the empty input and no-true sentinel, and derive O(log n) time from repeated halving and O(1) space.
The Python bisect reference(opens in a new tab) provides library boundary semantics. Cutting Ribbons(opens in a new tab) transfers the same proof to a numeric answer space.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Binary Search proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Binary Search complete workflow | O(log n) | O(log n) | A monotone predicate with one boundary is a direct lower-bound search even when the desired item is absent. Every index before left is known false, while every index at or after right is known true or the sentinel len(flags). |
O(1) for the focused Find the First True Boundary implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Verify algorithm preconditions such as sorted input, nonnegative weights, acyclicity, or admissible heuristics before applying it.
Prevent it: State and verify this precondition before coding: The search space and equality or monotone-boundary contract are explicit.
Avoid
def find_first_true(flags):
passUse instead
def find_first_true(flags):
left, right = 0, len(flags)
while left < right:
mid = (left + right) // 2
if flags[mid]:
right = mid
else:
left = mid + 1
return leftWhere you will hit this: Find the First True Boundary(opens in a new tab)
Uses -1 instead of the required insertion boundary when the monotone sequence contains no true value.
Prevent it: Preserve this proof obligation: Every discarded candidate or interval is excluded by a direct comparison or monotone predicate.
Avoid
def find_first_true(flags):
left, right = 0, len(flags) - 1
while left <= right:
mid = (left + right) // 2
if flags[mid]:
right = mid - 1
else:
left = mid + 1
return left if left < len(flags) else -1Use instead
def find_first_true(flags):
left, right = 0, len(flags)
while left < right:
mid = (left + right) // 2
if flags[mid]:
right = mid
else:
left = mid + 1
return leftWhere you will hit this: Find the First True Boundary(opens in a new tab)
Include visited state, boundary cases, recursion depth, and hidden copying costs in correctness and complexity analysis.
Prevent it: Count every sort, slice, copy, membership check, heap update, and recursive frame before claiming O(log n).
Avoid
def find_first_true(flags):
left, right = 0, len(flags) - 1
while left <= right:
mid = (left + right) // 2
if flags[mid]:
right = mid - 1
else:
left = mid + 1
return left if left < len(flags) else -1Use instead
def find_first_true(flags):
left, right = 0, len(flags)
while left < right:
mid = (left + right) // 2
if flags[mid]:
right = mid
else:
left = mid + 1
return leftWhere you will hit this: Cutting Ribbons(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27