Binary Search

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write search(sorted, target): sorted is an array of numbers in ascending order, with no repeats. Return the index of target, or -1 if it isn't there. Check the middle, then keep only the half that can hold target. The checks run 100,000 searches on a million numbers, far too many to look through one by one.

Your code must pass

  • Finds numbers anywhere in the array
  • Returns -1 for missing numbers
  • Handles empty and one-number arrays
  • Is fast enough for 100,000 searches