Lecture

Coding Quiz - Implement Binary Search

Based on what you have learned so far, implement the binary search algorithm yourself.

Given a sorted list of numbers and a target value, you need to write a function that uses the binary search algorithm to find the index of the target value.

If the target value is not present in the list, return -1.


Write the Code
def solution(numbers, target): # Write your code here return



Constraints

  • numbers are sorted in ascending order.

  • numbers do not contain duplicate values.

  • target is an integer.




Input and Output Examples

  • Input: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9], target = 4

  • Output: 3


  • Input: numbers = [10, 20, 30, 40, 50], target = 30

  • Output: 2

Lecture

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help