The pharmacist and the binary search
Just the other day I sought a pharmacy chemist1 to buy skin care products prescribed by my dermatologist to treat an annoying beard irritation which was not going away2. These products can be quite expensive here in Brazil, not sure if that is also the case elsewhere, so I was looking at them and realizing I would be spending several hundred Brazilian reais into just two or three products, but well, I needed the issue sorted, right?
In here, drugstores often offer you discounts at the counter just by providing your taxpayer’s ID number3, however in this case I was not willing to find out at the last moment, so I asked the pharmacist whether those prices were final, to which I heard:
Let me see how much discount I can give to you.
I was intrigued, not knowing they had this authority to just “arbitrarily” reprice the items, so she got to the computer, took on the “Math Lady”4 pose and started trying prices.

Picture generated by AI with the prompt “Create a cartoonish version of the Brazilian math woman meme, but instead of only equations, there are also skin care products” using ChatGPT.
I inspected closer and noticed she had scanned all three products into the store’s system and then opened a “discount tab”. There she could input any price for each item and have the system tell her that the discount had been exceeded or that the new price was valid.
If you know binary search, you are likely smiling at this point, but if you don’t, let me explain: one of the products had a hefty R$150 price tag, if she tried to lower its price R$1 by R$1, and the minimum price was R$120, she would need 30 attempts to get to it! Now by using binary search, she can try: 75, 112, 132, 122… and eventually get to R$130 with only 7 attempts5, four times less than she would have tried before. To my surprise, that was roughly what she was doing, probably unaware of the theory behind it, but intuitively trying to speed up this guessing game.
There are many real life situations where binary search is useful:
- Looking at recorded camera footage and trying to find a day and time where some visible change occurred (like when someone hit your car at the garage or stole something from your house).
- Picking the right drill for a hole or wrench for a nut.
- Finding a word in a dictionary 👴.
- …
I left there very happy, partly because I ended up getting a 20% discount6, partly for seeing another theory successfully applied. In a sense a “university-themed” continuation to my “What I learned in school and actually needed in life!”, but without knowing whether that woman had ever taken any algorithm lesson 🤣.
I couldn’t lose the chance of rewatching and sharing this Limmy’s show sketch. ↩︎
If you’ve been following me, I was in the USA a couple weeks ago and I had to present an awards ceremony, so I took the chance and shaved my beard with a razor, which is always a bad idea, as it gets me into this itchy state. ↩︎
In what actually turned out to be a scandalous scheme to track consumers and eventually led to some drugstores being fined. ↩︎
This is a very famous Brazilian meme that somehow became global! ↩︎
I won’t dive into how binary search works, please read its Wikipedia article which does a great job at explaining all its details or ask any AI for a quick step-by-step summary. ↩︎
Also partly because I saw someone trying their best to help a stranger, regardless of the business best interest (though I know for sure the pharmacy is not losing any money). ↩︎