1. How many cards can you rule out without looking?
Number fifteen cards from 1 to 15. Lay them out in increasing order, hide the numbers and ask a friend to find 11. Turning them over from the beginning gets you there on the eleventh turn. Open the eighth card first instead. It says 8. The seven cards to its left are still face down, yet you know none of them can be 11. The order you established before the game tells you something about even the numbers you cannot see.
Consecutive numbers make it easy to track which card you examined. Here you could point straight to the eleventh position, but if the values had gaps like 2, 5 and 19, their values and positions would differ. Binary search asks whether the target is smaller than, equal to or larger than the middle card. You only need to mark the first and last possible positions. You do not have to calculate where a particular number belongs.
2. Trace the path to 11
All fifteen positions are still in play in the top row of the diagram. Put your finger on the 8 with the heavy outline. Since 11 is larger, rule out 8 and everything to its left. Seven cards remain, from 9 through 15. In the second row, inspect the middle of those seven: 12. Now 11 is smaller. Rule out 12 and everything to its right, leaving 9 through 11. Each time, choose the middle of the interval you just kept.
In the third row the middle of 9, 10 and 11 is 10. Discard 9 and 10 because 11 is larger. Turn over the last remaining card as well to check that it really says 11. The four values examined were 8, 12, 10 and 11. The faded positions have stayed in place. Apart from the earlier middle cards, you ruled them out without reading them. Cover the discarded ends with two sheets of paper to reproduce the four rows without moving any cards.
3. How do you know a value is missing?
Leave the same fifteen cards in place and look for 10.5. You can compare the numbers even though the target is not an integer. It is larger than 8, smaller than 12 and larger than 10. Again only 11 remains. This last card is larger than 10.5, so remove it and everything to its right from the remaining interval. Its left boundary is now at the eleventh position and its right boundary has fallen back to the tenth. They have crossed. There is nowhere left to look.
Until the interval is empty, all you can say is that you have not found the target yet. Treating a single remaining card as an automatic match would announce that 10.5 had been found. Try the opposite test too: start with just one card and look for its number. Stopping when the two boundaries point to the same position would skip that card. Distinguish one candidate from none, and exclude a middle card that did not match so it cannot be examined over and over.
4. What if just one card is out of place?
Swap 3 and 11 in the original row. The middle is still 8. Following the same procedure, you cover all seven cards to the left because 11 is larger than 8. But the target is now in the third position, underneath the paper. Picking every subsequent middle perfectly will not recover it. The comparison of 8 with 11 was accurate. What failed was using that comparison to infer the values of the neighboring cards.
The rule used for sorting must match the rule used for searching. Write 2, 10 and 11 as strings and sort them lexicographically. They can appear as “10”, “11”, “2”. If you now look for the number 2 and drop the right side after comparing it numerically with the middle 11, you cover the final “2”. Sort by numeric value for a numeric search, or use the same text ordering both times. With a small pile in no known order you can also check each card in turn.
5. What if several cards match?
Lay out 4, 7, 7, 7, 9. The third card answers “Is there any 7?” immediately. It cannot answer “Where is the first 7?” by itself. Keep the third position as a possible answer while checking for an earlier match. The second position holds the first 7, and the fifth position is the first one past the run. Counting from the second up to but excluding the fifth gives three matching cards.
Python’s bisect_left finds an insertion position before equal values, and bisect_right finds one after them, preserving the order. Python counts positions from zero, so the second and fifth positions here are indices 1 and 4. Searching for 8 also gives an insertion position: both boundaries fall immediately before 9. A returned insertion position alone does not establish that 8 is present. Check that the position is within the row, then compare the card there with the target.
6. What exactly did four comparisons measure?
Just before each comparison on the way to 11, there were 15, 7, 3 and 1 candidates. If an even number leaves two middle positions, use the one on the left in this exercise. After ruling out the middle, even the larger side contains at most half the previous candidates. With 31 cards the first comparison leaves at most 15. Four comparisons suffice in the worst case with 15 cards, and five suffice with 31. A target at the first middle takes just one.
That count measures card comparisons. In a row with marked positions you can move your hand directly to the middle. In a stack you can only turn through from the top, reading the middle number once may involve moving several cards. Nor did the four comparisons include the initial work of sorting. When you try the game, tally the numbers read separately from the moves needed to reach them. The search can visit the same values while the storage arrangement changes the work involved.