Solution Review: Binary Search

Let’s take a detailed look at the previous challenge’s solution.

Solution

  • We can use binary search to search effectively when we have data arranged in increasing or decreasing order. We divide our search space in half at each stage.
  • We compare the middle value with the value we’re looking for at each point. We return true if the mid value is equal to the value we’re looking for.
  • If the value is smaller than the middle value, we search the left half of the array (if the array is arranged in ascending order). Otherwise, we search the right half of the array.
  • If we find the value we’re looking for, we return true. Otherwise, we return false.

Solution code

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.