Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Miguel
Clarification on binary search result

Miguel

May 11, 2024

I would like to see more careful wording on the explanation behind using binary search.

# Binary search method to find the index of the closest element to the target

Note that this method returns either mid or low-1, meaning it returns the index of the closest element that is less than or equal to the target, not necessarily the closest (given len(arr) > 1 and there are both smaller and larger numbers in the list).

ex: binary_search([0, 1, 100, 1000, 10000], 99) => returns index 1 (despite the fact that 100 is closer)

0

0

Comments
Comments

On this page