Skip to content
mlmentorship

Binary Search

Find a target in a sorted array.

Published · 3 min read ·Core ·Foundation

30-second answer map

Visual first · depth when needed

Preserve the inclusive sorted interval that can still contain target 11.

Preparing the visual…

ML breadth · active recall

Practice before you read

8 minutes. Explain the mechanism, why it works, when it fails, and one alternative.

How practice works

ML breadth · closed-book attempt

Explain the mechanism, why it works, when it fails, and one alternative.

08:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

Find a target in a sorted array.

Start with the concrete trace below. It shows the state the algorithm must carry as it runs.

Problem trace

Binary Search: Preserve the inclusive sorted interval that can still contain target 11.

Input and goalFind a target in a sorted array.
Initialize the candidate intervalFor target 11, left=0 and right=7 keep every index; middle=floor((0+7)/2)=3, whose value is 4.
left-90-3102middle4374115186right257
target11interval[0, 7]comparison4 < 11

Recognize it
The input is sorted and the task asks for an exact target position, so comparing one middle value can order the target relative to an entire half.
Keep true
Before every probe, if target 11 exists, its index is inside the inclusive interval [left, right]; every removed index is provably too small or too large.
Reuse it
For lower-bound, upper-bound, and insertion-position variants, first define what the interval promises, then choose whether middle is excluded or retained after each comparison.
Read it this way: For target 11, left=0 and right=7 keep every index; middle=floor((0+7)/2)=3, whose value is 4. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.

Pattern: Binary search on indices.

Simple idea: Compare the middle value with the target. Keep only the half that may still contain the target.

def binary_search(nums: list[int], target: int) -> int:
   left, right = 0, len(nums) - 1

   while left <= right:
      middle = (left + right) // 2
      if nums[middle] == target:
         return middle
      if nums[middle] < target:
         left = middle + 1
      else:
         right = middle - 1

   return -1

Cost: time and space.