Search HoldMyTwist

Browse all posts

Episode 3

Three Drawers Before the Bell

Duck has a ticket for his missing parade whistle. There are 7 drawers in the robot's workshop. Robot can only open 3 drawers before the bell goes off.

Episodes

Computing Basics

    Duck holds up a claim ticket marked 23 inside the aqua monitor. A brass bell hangs above the workshop.
    Robot faces the monitor. Beneath the bench are seven closed, unnumbered drawers with brass pulls.
    Robot gestures along the row of drawers while Duck watches from the monitor.
    Robot reaches for the middle drawer. The diagram marks the fourth of seven positions.
    Robot opens the fourth drawer and lifts its cream tag. It reads 19.
    Duck holds ticket 23. The diagram crosses out the first four positions and keeps the three on the right.
    Robot reaches for the middle of the three remaining handles. The diagram marks drawer six.
    Robot holds up the tag from the sixth drawer. It reads 35. Duck points back to the left.
    Robot pauses with a pincer to its chin. The diagram holds the results of both checks while the reader chooses.
    Duck studies ticket 23 with a wing at its bill. The next drawer has yet to open.
    Duck points to the one closed drawer still in play. The diagram marks position five.
    Inside the open fifth drawer, a brass whistle lies beside tag 23. Duck opens its beak in delight.
    Robot blows the recovered whistle as the bell rings. Duck kicks up one red boot inside the monitor.
    Duck shows a new ticket marked 31. The cabinet keeps its original order.
    The diagrams compare 31 with 19, then with 35. Only the intervening position remains to check.
    Robot holds adjoining tags 23 and 35. There is no spot between them for 31, and no candidate drawers remain.
    Robot imagines swapping tags 23 and 3. The thought bubble shows the swap while the physical cabinet stays closed.

    Episode transcript

    Ticket 23

    Robot spots a red-booted duck on the monitor. Duck's claim ticket number is 23.

    Duck: My parade whistle! Gotta have it before that bell goes off!

    He sees a row of 7 identical drawer fronts. They have no numbers.

    Duck: Drawer 23?

    Robot: I put tags inside the drawers. Left to right, the numbers increase.

    All seven drawers are closed.

    Robot: 3 tries before the bell. First, the middle drawer.

    The first two checks

    The robot draws open the drawer in the 4th position from the left. Inside it is a tag with the number 19 on it.

    Everything to the left of that is smaller.

    23 > 19

    Duck: Keep the three on the right.

    Robot: Try the one in the middle of the three.

    Starting from the left side of the rack, this is drawer number six.

    In the sixth drawer you can find tag 35. If you look at the drawer further to the right you will find an even greater number.

    Duck: 35 is way too big. Look to the left.

    The last check

    One side is the drawer with 19 in it. The other side is the drawer with 35. There's also one closed drawer in between.

    That's the 5th drawer from the left.

    The robot opens the drawer for him. There's the brass whistle.

    Duck: 23!

    The robot whistles into the workshop. A few seconds later the workshop bell chimes.

    Duck: Did the bell just respond to your whistle?

    Ticket 31

    The tags are in the same order.

    For ticket 31 it's back to the middle.

    31 > 19 (right)

    31 < 35 (left)

    Check 3 (drawer 5)

    The robot opens the drawer for him. It sees a claim tag inside which says 23.

    31 > 23

    23 eliminates drawer 5 and all the drawers left of it for ticket 31. The robot also knows drawer 6 has 35 from the last try, so it's already eliminated 6 and 7. There's no fourth drawer to pull open in the duck's search for ticket 31.

    Duck: The next tag after 23 is 35. No space for 31.

    Robot: If you mix your tags then you may miss the drawer you want.

    Binary search

    Binary search tests the middle value in the range of a sorted list where a match may exist. If the middle value is a match, then stop searching. If the middle value is smaller, then that value and everything on the left of it can't be a match. If the middle value is larger, then that value and everything on the right of it can't be a match. Test the remaining part using the same process.

    If the part of the list that is still in the search area is empty, then the search ends. Every element in this sorted list has been eliminated.

    3, 8, 14, 19, 23, 35, 52

    If you start from the extreme left, it will take you 5 drawers to get to the whistle. If you do binary search, you'd get the same whistle in 3 drawer openings: 4th, 6th, and 5th.

    What if tags were jumbled?

    23, 8, 14, 19, 3, 35, 52

    But the tag in the middle drawer is still 19. The robot will still turn right and look for 23. We could miss a matching tag by performing the same search.

    If you check the middle drawer first, you can only eliminate one side if the tags are ordered.

    Sources

    Paul E. Black, “binary search”, NIST Dictionary of Algorithms and Data Structures (opens in a new tab)