LESSON 72 / 75·2 min readIntermediate

Binary search and greedy in Python

Good to knowConcept lesson
01 Understand02 Recall03 Prepare
Python
def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        if arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

Greedy examples

  • Jump Game
  • Assign Cookies
  • Activity Selection
  • Minimum Number of Coins

PAUSE & RECALL

Can you explain it without the notes?

Binary search and greedy in Python

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Binary search

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Sliding window and two pointers in Python

Adapted from 18_DSA_PYTHON.md · “11. Binary search and greedy in Python”. Examples are study fragments; framework behavior can depend on your version.