Understanding Algorithms: A Step-by-Step Guide
Algorithms are a set of instructions used to solve a problem or accomplish a task. Learn more about algorithms with this step-by-step guide inspired by a lesson from the Computer Science Fundamentals by @brilliantorg.

Francesco
· Developer Advocate at @dailydotdev · Docker Captain · Public Speaker · Building a 1 Million Community 23% · All the links ➞ https://t.co/2DLpQ5cNoa

-
In programming, the word "algorithm" might sound scary.
— Francesco (@FrancescoCiull4) March 18, 2023
But it really isn't.
It's a set of instructions or a step-by-step procedure used to solve a problem or accomplish a task.
Let's see, for example, the "Divide et Impera" (Ancient Romans applied this to military strategy!) -
This thread is inspired by a lesson from the "Computer Science Fundamentals" by @brilliantorg, a platform that teaches Mathematics, Computer Science, and more.
— Francesco (@FrancescoCiull4) March 18, 2023
Lessons are bite-size and interactive to help you learn.
Check it out for free at https://t.co/xaHSNNArQQ pic.twitter.com/rid6AxPdpu -
In 20 Questions, one player chooses an object or a famous person, while the other asks up to 20 yes-or-no questions to try to guess the object.
— Francesco (@FrancescoCiull4) March 18, 2023
If the questioner correctly guesses the object within 20 questions, they win. -
What strategies can the questioner and computer use in a simpler 20 Questions version with 200,000 Wikipedia performers?
— Francesco (@FrancescoCiull4) March 18, 2023
One strategy is to ask random yes-or-no questions about actors, such as:
"Is it Obama?"
or
"Is it Jackie Chan?" -
20 Questions is limited to 20 questions to avoid the game taking several weeks to complete.
— Francesco (@FrancescoCiull4) March 18, 2023
Listing the top 20 famous people is unreliable as the answerer might think of someone less known.
One-by-one guessing is only advisable when there are at most 20 choices. -
In 20 Questions, the questioner can ask about multiple traits, such as where they live or their hair color.
— Francesco (@FrancescoCiull4) March 18, 2023
Asking "balanced" questions that divide people into similar-sized groups is effective.
This technique, known as "divide et impera" is widely used in computer science. -
By Wikipedia, there are ~200000 actors.
— Francesco (@FrancescoCiull4) March 18, 2023
The questioner aims to ask a question that eliminates many actors/actresses from the list of 200000 on Wikipedia, regardless of whether the answer is "Yes" or "No."
A visual representation: pic.twitter.com/of1HFNXk10 -
Asking if the actor is male is an excellent way to divide the candidates into ~half.
— Francesco (@FrancescoCiull4) March 18, 2023
Asking if the performer is Indian may also narrow down the field, but not by much.
Asking if the performer played James Bond is not helpful since only a few have played that role. -
Animated game of 20 Questions with the answer "Janelle Monáe".
— Francesco (@FrancescoCiull4) March 18, 2023
Questions divide the remaining group of performers in half, and eliminated performers are shown in grey. pic.twitter.com/55l0aRoT3e -
Graph of a game of 20 Questions with questions on the horizontal axis and the number of remaining performers on the vertical axis. pic.twitter.com/jOvKCDx154
— Francesco (@FrancescoCiull4) March 18, 2023 -
The graph becomes more evident if the vertical axis is logarithmically scaled to represent the number of actors.
— Francesco (@FrancescoCiull4) March 18, 2023
The initial performers correspond to a bit less than 18 on the vertical axis. pic.twitter.com/ACL3v5xYWr -
Each question in the game of 20 Questions tries to balance the number of performers eliminated by a "Yes" or a "No"
— Francesco (@FrancescoCiull4) March 18, 2023
This ensures the questioner can rely on something other than luck to eliminate many performers or risk ending up with only a tiny fraction of the original number pic.twitter.com/BqPw1lyZ2P -
Here is another graph:
— Francesco (@FrancescoCiull4) March 18, 2023
Asking questions that perfectly divide the candidates in half results in a graph with a nearly straight line. pic.twitter.com/zlvda0NBZZ -
Consider breaking away from the perfect balance of questions if prior knowledge about the answerer's preferences exists.
— Francesco (@FrancescoCiull4) March 18, 2023
If the answerer is a Bollywood fan, ask if the performer is an Indian citizen.
But the answerer may anticipate this and avoid choosing an Indian citizen. -
Simulating 20 games of 20 Questions with 50-50 splits produces similar results, but the games are different.
— Francesco (@FrancescoCiull4) March 18, 2023
The number of answers might vary from 16 to 19
Why? pic.twitter.com/Bq71mV9XzB -
Imagine a small guessing game with three possibilities:
— Francesco (@FrancescoCiull4) March 18, 2023
The first question can only split into one group of 1 and 2.
You can guess the answer immediately if the performer is in the smaller group, otherwise you'll need one more question to determine the answer. -
Another strategy besides asking 50-50 questions is to ask 25-75 questions.
— Francesco (@FrancescoCiull4) March 18, 2023
These questions split the candidates into one group with 25% and another with 75%, for example, dividing 200,000 performers into groups of 50,000 and 150,000. -
Taking this idea to the extreme won't work.
— Francesco (@FrancescoCiull4) March 18, 2023
You'd be back to picking out individual performers and asking about them, the strategy rejected earlier.
25-75 questions can lead to lucky guesses that dramatically reduce the number of candidates but often leads to unlucky guesses. -
Splitting the candidates in half with 50-50 questions makes the game more consistent and faster.
— Francesco (@FrancescoCiull4) March 18, 2023
Asking 25-75 questions can result in a worst-case scenario of 39 and a best-case of 9 questions to find one performer out of 200,000.
On average, it takes 21.2 questions. -
The graph shows how different question balances affect the worst case, best case, and the average number of questions needed to identify one out of 200000 possibilities.
— Francesco (@FrancescoCiull4) March 18, 2023
Asking 50-50 questions results in 18 questions in the worst case, 17 in the best case, and 17.7 on average. pic.twitter.com/XYA9ibiAxr -
This proves how using 50-50 splits in 20 Questions is the most consistent and efficient strategy if you have no extra information about your opponent's guess or if you value a consistent number of questions.
— Francesco (@FrancescoCiull4) March 18, 2023 -
Conclusion:
— Francesco (@FrancescoCiull4) March 18, 2023
The "divide et impera" strategy in computer science can be applied in the game of 20 Questions.
Asking 50-50 questions that divide the group of candidates most evenly is the most consistent and fastest strategy, even though there are other ways to play the game. -
As I mentioned, this was taken from @brilliantorg 's course on Computer Science.
— Francesco (@FrancescoCiull4) March 18, 2023
If you're going to succeed in tech, it's crucial that you understand these topics, and Brilliant will help.
Get 30 days free + 20% off an annual plan with my link: https://t.co/xaHSNNArQQ -
IF this thread has been useful, follow @FrancescoCiull4 and share the tweet below.https://t.co/PZLaVZnXAP
— Francesco (@FrancescoCiull4) March 18, 2023