Binary Search: Is this food hot enough for my consumption?

Let’s talk about a classical computer science algorithm and how it can help you avoid getting burned or your jaws mutilated from teeth chattering on a cold morning. It can also help you quickly find words in a dictionary (if you still have one lying around), and solve many other daily life issues with minimal effort.

We’ll use searching a word in a dictionary as an example. Searches often have a “search space”, which are the confinements of where our answer could be. At the beginning, the whole dictionary is our “search space”. Our aim is to keep reducing this search space until we reach the answer. One way is to open a random page X, and if the word we’re looking for is supposed to occur before page X, then our search space is reduced to the first X pages of dictionary; if after, then it’s the rest of the dictionary. We can keep repeating this process until we find the word.

Now the question that begs an answer is, what’s the “best” way of choosing this random page in our “search space” so that we’re risk-averse and are guaranteed to find the answer without scanning every page of the dictionary. Intuitively, smaller the “search space”, faster we can find our answer. If we want to quickly reduce the size of the search space, and not be dependent on luck, we can cut it in half by simply picking the middle page. This is typically very fast and guaranteed to find your answer in log(number of pages) steps (which basically means how many times we have to multiply 2 to reach the number of pages).

This algorithm is famously known as binary search, and it can be applied at many places in our lives — we just have to ask the right question

  • answer to which needs to be a “yes” or “no”.
  • this method works only if the answer to this question tips at a single point in our search space (which is often our answer).

It may seem slightly complex — so let’s see how it applies to search a word in the dictionary. You choose a point in search space, and the question needs to framed as “does my answer word lie before this point?”. Note that the answer to this question is “yes” for all the points before our answer, and “no” for all the points in search space after our answer.

Ask away, “Is the shower temperature less than what I want?”, “Is this food hot enough for my consumption?”, “Is X hours of sleep enough for my productivity levels?”, “Is X minutes of microwave heating enough to warm my coffee?” and many other questions that require you to record observations and calibrate.

Software Engineer @Vedantu, Former Intern @Hackerrank, GSoC @Wikimedia, Codeforces(Expert)