Binary search and sorting · Problem 1 of 3

Find a booking number in a sorted list

Pattern: Binary Search in a Sorted Array

easyNot solved yet

The problem

A hotel keeps booking numbers in a list sorted from smallest to largest, with no duplicates. Write a function that returns the index of target in the list, or -1 if it isn't there. Your solution should take O(log n) time, so don't scan the whole list.

text
1find_booking([102, 205, 318, 440, 517], 440)  ->  3
2find_booking([102, 205, 318, 440, 517], 300)  ->  -1
3find_booking([], 7)                           ->  -1
Example 1: finding 4401 / 2
lowmidhigh
102
0
205
1
318
2
440
3
517
4

318 < 440, so 440 can only be to the right.

Write find_booking(bookings, target) in Python or findBooking(bookings, target) in JavaScript.

Runs privately in your browser. Nothing is sent to a server.Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Write your function, then run it against 22 tests.

Breakdown

Stuck? Reveal one step at a time, and try again after each one.

  1. Step 2
  2. Step 3
  3. Step 4

Solution

?

Give it a real try first: you learn far more from a wrong attempt than from reading the answer.