Interview Prep: Binary Search
Edaqa Mortoray
26:02
More details
Description
A quick refresher of this common programming algorithm. Perfect for interview preparation.
In this class, I look at the binary search algorithm. I walk through some samples and provide the basic complexity analysis. In addition to binary search, I explain lower and upper bound searching. Youâll learn what they are and their purpose.
Project
I want you to write a binary search function. Pick a language and write a function that takes an array of values, and an item to look for. The function returns an optional value, the index of where the item is found, or nothing if it wasnât found. If youâre unfamiliar with optional values, return a `-1` instead, should the value not be found.
In the example Python implementation, the signature looks like this:
```py
def binary_search( value : ComparableT, items : Sequence[ComparableT] )
-> Optional[int]:
```
You should assume the array is sorted.
Upper and Lower Bound
For extra credits, write the upper and lower bound functions.
`lower_bound`
returns the first index where the item in the list is not
less than the input value.
`upper_bound`
returns the first index where the item in the list is
greater than the input value.
For all the algorithms, I provide a walkthrough of sample code. The code is available for you to review.
Soure Code: https://github.com/mortoray/interview.codes/tree/master/binary-search
User Reviews
Rating
Edaqa Mortoray
Instructor's Courses
SitePoint
View courses SitePoint- language english
- Training sessions 7
- duration 26:02
- Release Date 2023/09/25