Yet another riddle that I’ve done a poor job naming. This is a riddle I heard while I was in Game Theory class several years ago, I take no credit for it other than butchering the actual details.

This article is going to be long. But here’s whats in it:

The riddle

Some things to keep in mind

Some common responses

A bunch of hints

Hints that lead into an explanation

A concise answer

It goes something like this:

Somewhere in the world there is a small village of people who live on a tiny island. They have a population of only 100 people, but every single citizen has perfect logic, perfect memory, perfect hearing, and perfect vision. Life is good on the island. However, one day a powerful and cruel demon appears and declares that he will kill the inhabitants. But unfortunately for him there are rules that he must follow. He has to let the citizens play for their lives. He must line up all the citizens single-file so they all face the same direction. He must then go about putting black hats and white hats on their heads (he does it randomly, so there is no set number of white or black hats). The citizens are not able to move, but they are able to see every person in front of them, as well as every hat they wear. The citizens are not allowed to gesture or speak until the demon gets to them, and they cannot see behind them. However, they can hear what the others say. The demon then starts at the back of the line and asks “what color of hat are you wearing?”. To which the citizen is allowed to say only “black” or “white”, and nothing else. If they are correct, they live. If they are incorrect, they die. The demon gives them a night to congregate and talk out their plan. The ritual starts in the morning. What should the citizens do?

Whew, what long riddle. No doubt you’re a bit unsure of where to start.

Here are some things to consider:

The citizens are essentially unable to act until it comes to their turn. When it arrives at their turn, the only thing they are able to do is say “black” or “white”. So whatever you do, the response of “black” or “white” has to be telling.

The citizens are basically super-human. So if you think of an answer that wouldn’t work for normal humans, you might actually be okay.

COMMON STRATEGIES (possible answers you may have come up with)

Given that, here are some ideas that I’ve heard which are good, but not the best response:

Strategy 1: Say the color of the hat of the person directly in front of you. While everyone things this plan is brilliant at first, unfortunately it doesn’t save as many people as you might think.



Lets say these are the hat colors of the first 6 people: Lets say these are the hat colors of the first 6 people: BWWBWB

B= black hat

W= white hatHere is how it would play out:1. first person(B) says “white” since the next citizen is wearing a white hat- dies

2. The second person(W) says “white” because he knows his hat color is white- lives

3. The third person(W) says “black” because the next person is wearing black- dies

4. The fourth person (B) says “black” because he knows he is wearing black- lives

5. The fifth person says “black” because the next person is wearing black- dies

6. The sixth person (B) says “black” because he knows his hat is black- lives As we can see, every other person will live, and if the person in between is lucky, he gets to live too. With this method you will save somewhere above 50% of all citizens. The method is not terrible, but given the circumstances there are better methods.

Say the color of the hat of the person directly in front of you. While everyone things this plan is brilliant at first, unfortunately it doesn’t save as many people as you might think. B= black hat W= white hatHere is how it would play out:1. first person(B) says “white” since the next citizen is wearing a white hat- dies 2. The second person(W) says “white” because he knows his hat color is white- lives 3. The third person(W) says “black” because the next person is wearing black- dies 4. The fourth person (B) says “black” because he knows he is wearing black- lives 5. The fifth person says “black” because the next person is wearing black- dies 6. The sixth person (B) says “black” because he knows his hat is black- lives Strategy 2: Another interesting response I’ve heard is, “The first person whose turn it is, will pick the color that has the majority”.So the first person will look at the number of black and white hats, and see which color is more popular. He would then say “white” if white is the dominant hat color. The rest of the people in line would also say that color. So it would basically be responding with all white, or all black. This is an interesting strategy since it guarantees that at least the majority will survive (51% and upwards). Depending on the demographic of black hat wearers and white hat wearers, this could do better than the previous answer. But is not the best.

Another interesting response I’ve heard is, “The first person whose turn it is, will pick the color that has the majority”.So the first person will look at the number of black and white hats, and see which color is more popular. He would then say “white” if white is the dominant hat color. The rest of the people in line would also say that color. So it would basically be responding with all white, or all black. This is an interesting strategy since it guarantees that at least the majority will survive (51% and upwards). Depending on the demographic of black hat wearers and white hat wearers, this could do better than the previous answer. But is not the best. Strategy 3: In this strategy, every person will look at the line ahead of him and say the most common color of hat. This is essentially a mix of the two strategies above. The thought behind it is that in case there is a block of black hats in a majority of white hats such as WWWWWBBB, nearing the end you will be able to account for those black hats with perfect accuracy; which is something strategy 2 had issues with.However great this strategy seems at first, it is ultimately flawed. I’ll demonstrate it with an example:If you have this pattern: BWBWBWBWBWBWWith an equal number of black and white hats, you will end up saving no one:

The first person will see a majority of white hats and say W (and die), the second person will see a majority of black hats and say B (and die), this process continues until everyone dies.

HOW TO THINK ABOUT IT (Let’s walk through the thought process):

*WARNING* THE MORE OF THIS YOU READ THE CLOSER YOU WILL BE TO HAVING IT SPOILED. READ AT YOUR OWN RISK

So now that you’ve seen two common answers, lets take a look the workings of the problem from the simplest iteration and work our way up, and solve it a step at a time.

Test 1: Only having a single citizen

What would the one citizen know? Frankly nothing. He knows he is wearing a hat, but has no clue what color it is. The best he can work with is a guess. There’s not much we can do to account for Citizen 1’s survival.

Test 2: Only having 2 people in line

What has changed in this instance for our citizens? Citizen 1 has changed in that he is able to see the color of the hat that Citizen 2 is wearing. But still can’t see his own. Citizen 2 can’t see his own hat, but knows that Citizen 1 can tell him. A good strategy here is Strategy 1. Citizen 1’s life can’t be guaranteed, but Citizen 2’s can.

Test 3: Having 3 citizens



What has changed now? Citizen 1 now knows the color of the hat of two citizens ahead. Citizen 2 now knows the color of citizen 3. There are 3 people who can account for the hat color of citizen 3 So how can we guarantee the lives of both citizen 2 and citizen 3?

Well what do we know?

Well, we know that we can’t really do much for citizen 1.

We know that if we want to save citizen 2, it has to be through something said by citizen 1

We know that to save citizen 3, it needs to be something said by citizen 1 and/or 2.

We know that strategy of Citizen 1 informing Citizen 2, who informs Citizen 3 is not foolproof and doesn’t guarantee the life of Citizen 3.

So what do we have to do?

*WARNING* SLIGHT HINT BELOW (though you might want to take it)

Well since the personal and direct informing method doesn’t work (basically strategy 1), maybe the responses need to be indirect. So instead of saying “Citizen 2, your hat is *this color*” we need to say something like “Hey guys, this is what I see”.

Well, what does Citizen 1 see? And how could he say things about what he sees?



*WARNING* HEAVIER HINT BELOW

Say the layout of the hats are: WBB. Citizen 1 (wearing a white hat) see’s two black hats in front of him. What can he say about that:

There is a majority of black hats

There is an even number of black hats

Well the first observation is acted upon by Strategies 2 and 3 above. What about the second observation? Could we meaningfully say something by saying there are an even number of black hats in front of him?

Well, what if we made up a code that all the citizens knew? For instance, saying “black” means there is an even number of black hats, and “white” means there is an odd number of black hats. With that, we could say “I see an even number of black hats”.

So we have our code down, now what? What happens with Citizen 2?

*WARNING* THE ANSWER IS BASICALLY EXPLAINED BELOW (stop reading if you don’t want it spoiled)

Think about this: What is the difference between Citizen 1 and 2? Well Citizen 1 sees everything Citizen 2 sees, except he also knows Citizen 2’s hat color. Basically, Citizen 2 will see 1 less hat (either white or black) than citizen 1, the question is, which color is it?

Well if Citizen 1 sees an even number of black hats, and Citizen 2 is wearing a black hat, then Citizen 2 will see one less black hat. If Citizen 2 is wearing a white hat, then he will see the same number of black hats as Citizen 1.

Essentially if you see a different number of black hats as the person behind you, then you are wearing a black hat. If not, then all you are wearing a white hat.

So how does that help us? Well let’s play it out:

Let’s say that all the citizen’s know of the odd and even code ( if citizen 1 says “black” that means he sees an even number of black hats, and if he says “white” it means he sees and odd number of black hats) Citizen 1 says “Black” (which in this case basically says there are 2 black hats) Citizen 2 notices that what he sees, and what citizen 1 saw is different. Citizen 2 realizes that Citizen 1 had saw a black hat on his head. Citizen 2 says “black”

Alternate test:

Let’s say for instance the set up looked like this instead: WWB. How would it play out?

Citizen 1 would say “white” designating that there are an odd number of black hats (in this case only 1 black hat) Citizen 2 realizes that he sees the same number of black hats as Citizen 1 Citizen 2 realizes that the black hat Citizen 1 saw, must not be on his head Citizen 2 says “white” Citizen 3 thinks to himself “Citizen 1 said there was a black hat in between myself and citizen 2. Citizen 2 is wearing a white hat. So I must be wearing the black one” Citizen 3 says “black”

So what do we learn or notice from this alternate example? Well, Citizen 2 saw the same number of black hats as Citizen 1, and he responded “white”. With the original test case, citizen 2 responded “black” when he noticed his observations were different than citizen 1.

So I guess we have a bit of a code:

If you see the same number of black hats as the person behind you, say “white”

If you see a different number of black hats as the person behind you, then you are wearing the black hat- say “black”.

Let’s add another person

Test 4: 4 Citizens

Lets just jump into it and say that the line looks like this: WBBW. Let’s assume that everything is just like test 3, except there is now an extra person on the line.

Continuing from where we left off with test case 3, lets see how it would play out with our set up:

Citizen 1 says “black” (designating an even number of black hats) Citizen 2 sees an odd number of black hats and realizes that he must be wearing a black hat- says black Citizen 3 sees an even number of black hats, and realizes that must be wearing a black hat- says black Citizen 4 knows that he has a white hat. Citizen 1 had see an even number of black hats, which meant that out of the three of them there was either (0, or 2 black hats). Given the circumstance, Citizen 2 and 3 were wearing the 2 black hats, leaving Citizen 4 with a white hat.

Welp, that’s basically the way to the answer it. You can continue expanding the number of participants nigh indefinitely.

Definitely a hard riddle.

Answer (SPOILERS)

This is the concise answer. If you want to learn more about how it works, look at the explanation above.

The best method guarantees 99% of the people will live, with a chance to save the first person. The way it is set up is so:

They create a code that only the first person will follow:

If Citizen 1 sees an even number of black hats, he will say “black”

If Citizen 1 sees an odd number of black hats, he will say “white”

The next Citizen, Citizen 2 will look in front of him and see if he sees an even number, or an odd number of black hats.

If he sees the same thing as Citizen 1 (i.e. the number of black hats has not changed), then he knows that he is wearing a white hat. However, if the number has changed, then the black hat he did not see is on his head.

Thus, if the number changes, he is wearing a black hat.

Citizen 3 thinks back at Citizen 1 and 2’s responses. Let’s assume that Citizen 2 said “black”, which would mean that he saw a different number of black hats than Citizen 1. There are 2 scenarios:

Citizen 1 saw an even number- Citizen 2 saw and odd number of black hats. Assuming Citizen 3 is the last, then he must be the one wearing the last black hat.

Citizen 1 saw an odd number- Citizen 2 saw an even number. Assuming Citizen 3 is last, there must not be any black hats left, and he must be wearing a white hat.

Edit: A reddit user pointed out that this part may be a bit confusing. It should be said that there are essentially two “codes” being used. The first code is the one used by Citizen 1, to tell all the other citizens of whether he sees and even or odd number of hats.

The second code is used by all the other citizens. If the number of black hats they see is different from the number their predecessor saw, they will say “black”. If the number is the same, then they will say “white”.

You can repeat this process with any number of participants.

Whew, that’s all for now folks. Hope you enjoyed/understood this.