Skip to main content
Practice

coding-quiz-2

---
id: coding-quiz-2
title: Coding Quiz - Implement Binary Search
description: Write a function to find the index of a target value in a list using binary search.
tags:
- Coding Quiz
- Binary Search
- Python
sidebar_position: 6
isPublic: false
---

# 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`.

<br />

```python title="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

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.