The Hat Puzzle (A Consulting or Engineering Interview Brain Teaser)

The Hat Puzzle is one of my favorites. This is one of those brain teasers people might ask in an interview for a consulting or engineering job.

Here is the problem, as worded by Sara Robinson (this is a good article to read):

Three players enter a room and a red or blue hat is placed on each person’s head. The color of each hat is determined by a coin toss, with the outcome of one coin toss having no effect on the others. Each person can see the other players’ hats but not his own.

No communication of any sort is allowed, except for an initial strategy session before the game begins. Once they have had a chance to look at the other hats, the players must simultaneously guess the color of their own hats or pass. The group shares a hypothetical $3 million prize if at least one player guesses correctly and no players guess incorrectly.

The general problem is to find a strategy for the group that maximizes its chances of winning the prize.

(In the language of game theory, this is a simultaneous coordination game of incomplete information.)

If the players could talk after they had hats on, the game would be trivial. Since the players have incentive to cooperate, they would honestly reveal what they see. In a matter of seconds, they could all figure out their hat colors and guess correctly.

But the rules only allow for an initial strategy session. How well can the players fare? Amazingly, they can do pretty well.

The Basic Strategy (50 percent winning)

The rules heavily penalize incorrect guesses. A single incorrect guess makes the group lose—even if the other two players guess correctly. A single incorrect guess is the apple that spoils the bunch.

So it’s important the rules allow for players to pass. If a player doesn’t have a good guess, it would be a good idea to pass.

A basic strategy would be to minimize the risk of bad guesses. Force two players to pass in every game and make one person the official guesser. The group wins exactly when this person guesses correctly.

hat puzzle one person guess

How often will the group succeed? Since the hat color is chosen by a coin flip, there is a 50 percent chance of guessing the correct color.

This is not too shabby when there’s a $3 million dollar reward at stake. I will take that gamble any day.

If I were conducting an interview, I would be satisfied if an applicant, never having heard the puzzle, came this far. The answer demonstrates basic risk management and an understanding of probability.

But can the team do better than random chance?

If you’ve been reading my game theory articles, then you would guess that they can. Players can do often do better than random chance if they consider the distribution of outcomes—a tactic I described in my article about finding true love.

The trick is figuring out the players do have a way of coordinating as a group. Doing this, they can make winning an amazing 75 percent chance. Let’s investigate why.

One Optimal Strategy (75 percent winning)

Motivating question: does observing the other two hat colors tell you anything about your own hat color? In other words, if you see two red hats, does that make your hat more likely to be blue?

The answer is no, and that’s a potential roadblock. Regardless of what you see, your hat color is determined by a coin flip. Fair coins are never “due” for a particular outcome—each toss is independent.

But don’t get caught up in probability—the fact is that seeing the other hat colors does convey information. The problem is the figuring out how to transmit that information to the other players.

To get around that, players need to coordinate guesses based on what they see. If possible, they still want to minimize bad guesses by having two people pass and one person guess. What’s needed now is a group strategy.

How can they do that? It starts by taking a step back and considering the possible distributions of hat outcomes. With three players and two hat colors, there are a total of eight equally likely outcomes:

hat puzzle eight outcomes

Is there anything special about the distribution?

One feature is that most outcomes—six of them—include at least one hat of both colors. Only two extreme outcomes don’t—the ones with all red hats or all blue hats.

We can analyze further. Among outcomes with both hat colors, there logically has to be two hats of one color (the “majority” color) and one hat of another color (the “minority” color).

hat puzzle eight outcomes minority majority colors

Here’s the kicker: by looking at the other hats, players can identify whether they are wearing a majority color or a minority color.

For instance, if a player sees both a red and blue hat, then the player must be wearing the majority color (which could be red or blue).

If a player sees two blue or two red hats, then the player must be wearing the minority color, which will be the opposite color of what the player sees.

Here is what players can reason among the six choices:

hat puzzle eight outcomes minority majority guesses

Now the idea is to get the player with the minority hat color to guess and force the other people to pass.

So here is the strategy:

  • if you see both a red and a blue hat, then “pass”
  • if you see two red hats, then guess “blue”
  • if you see two blue hats, then guess “red”

This strategy wins in all six cases with at least one hat of each color. It only loses in the two cases of all-red or all-blue, in which all players guess incorrectly.

Here is how players would guess:

hat puzzle eight outcomes winning strategy

All told, the group wins in six of eight possible outcomes—a whopping 75 percent chance.

Optional Extra Credit: The Host can Learn

If you’re playing rock-paper-scissors against a computer that mixes randomly, you could win 1/3 of the time simply by picking one strategy, say rock. But if the computer could learn and analyze your pattern, it might respond by countering with paper and start winning a lot. To maintain your 1/3 winning chance, you need to randomize your choices among rock, paper, and scissors.

In the hat game, the players have a 75 percent chance of winning, but the strategy has a pattern. It loses every time the hat colors are all the same. A responsive host, like the computer in rock-paper-scissors, would see the pattern and respond by assigning hats to be the all one color more frequently. To keep the host honest, the players need to randomize.

Is there some a way the players can win, without creating a pattern of outcomes in which they all lose?

Amazingly, yes there is! Even more surprising, the winning percentage stays at 75 percent.

In researching this article, I learned about the claim and a proposed winning strategy from a presentation on Michel Waldschmit’s website called “Coding Theory, Card Tricks and Hat Problems.”

Unfortunately the slides didn’t contain the proof, or an example, so I felt a little bit like I was staring at the margin of a textbook from Fermat.

I took out my own pencil and paper and worked on the answer. I am happy to say I came up with the following analysis myself.

The Random Optimal Strategy (75 percent winning)

The random strategy is a refinement on the static one given above. The key to the above strategy is that players essentially bet against the outcome being all-red or all-blue. Knowing that, it was possible to coordinate guesses so only one person guessed and gave a correct answer.

There’s nothing special about picking all-red or all-blue.

The players can randomly pick any color combination and its “opposite” configuration (red-blue-red and blue-red-blue are opposites) as outcomes to bet against. The remaining six outcomes can be coded based on the hat colors that each player sees.

Why would this work, and why does it have to be “opposite” combinations?

The eight outcomes of the hat game can be visualized as vertices of a cube. Adjacent vertices differ by changing only a single “coordinate,” that is, the color of one player’s hat.

hat puzzle eight outcomes visual cube interpretation

The graphical interpretation is as follows: can the players identify which vertex they belong to? The information they are given is the other two hat colors they see—that is, they are effectively placed at midway points along the adjacent edges.

Each player can see the coordinates of the other two players, but is unsure about the own coordinate—that is, the player is unsure which of the two possible endpoints the group belongs to.

hat puzzle eight outcomes visual cube interpretation example

We want a situation where exactly two players will not be able to tell the vertex (they will “pass”) and the remaining player will know the location (and guess correctly).

Such a unique coding occurs when players bet against a random vertex and the “opposite” one—a limitation that gives maximal location information.

In any of these cases, players only lose if in fact the outcome is one of the two they bet against, meaning they have a 75 percent chance of winning.

The 75 percent chance of winning applies to every time the game is played but the losing outcomes are randomized. Hence, the host won’t be able to exploit any particular color combination.

Appendix: The coding for betting against red-blue-red and blue-red-blue outcomes (others can be found similarly)

Strategies:

Player 1:
If see blue-red, then pick red.
If see red-blue, then pick blue.
Else pass.

Player 2:
If see red-red, then pick blue.
If see blue-blue, then pick red.
Else pass.

Player 3:
If see red-blue, then pick red.
If see blue-red, then pick blue
Else pass.

Share this post:

| More

Previous post:

Next post:

Other posts you may enjoy reading:



  1. 12 Responses to “The Hat Puzzle (A Consulting or Engineering Interview Brain Teaser)”

  2. I love your posts, this is one of my favorite blogs. Please keep up the great thought provoking posts, we your readers love this stuff!

    By jb on Apr 16, 2008

  3. Hello!!!
    As I said these posts are exceptional.
    I really enjoy them!!!
    Keep walking!!!

    Dimitris Kanoulas

    By par...alogos on Apr 16, 2008

  4. jb and Dimitris Kanoulas: Thanks for the encouragement :)

    By Presh Talwalkar on Apr 16, 2008

  5. That is a neat extention of the hat puzzle. Please feel free to point out any gaps in logic in the following. A truly brilliant host would make a similar analysis and change the game so the order of the players was not discernable– red-blue would be indistinguishable from blue-red. This forces the adoption of the only order neutral solution, the basic 75% strategy (call this strategy A). At which point, in the single game, the host should provide rrr/bbb hats. Assuming a repeated game, what is the host’s strategy? By knowing the 75% strategy the players will use, the host can skew distributions of hats against the players, but if the players start losing too often, their strategy will shift. If the host always provides rrr/bbb hats, the players can simply choose the hat color they see the other players wearing (call this strategy B). If the host skews hat provision towards rrr/bbb, the players will choose either strategy A or B, depending on the skewing (rrr/bbb provided 60% of the time? Strategy B). Am I right in saying that the long term player win percentage is 50%? The host will use a mix of 50% rrr/bbb and 50% other solutions randomly. This way the players choice of strategies give equal chances of success. Interestingly, at this point the non-optimal solution you started off mentioning of one player randomly guessing is now equally optimal. Also, depending on paths to this long-term state, it might not happen at all– there might instead be a boom/bust players/host strategy cycle. As an example, assume that the host can either provide entirely random hat selection or rrr/bbb (still assuming player order is unknown, so only one 75% strategy exists), and that when players win more than 50% of the time, the host switches strategies. Player win percentage would cycle as follows: 75% -> 0% -> 100% -> 75%… with an average win percentage of approximately 58%.

    By David on Apr 16, 2008

  6. David: Excellent answer. You have put much thought into this! Here is my reaction.

    1. In the standard hat puzzle, I’m not sure how the host could hide the order of players. Three players could arbitrarily label themselves before the game starts (I’m player 1, you’re player 2, and he’s player 3…).

    2. To hide the player order, the host would have to blindfold players and tell each person information, like “the other two players have red hats” or “there is one red hat and one blue hat, but I’m not telling you who has what.” Such a change would make it a new game entirely, so let’s think about strategy for that game.

    3. So here’s the new game:

    Host–can pick rrr/bbb or random
    Player–basic 75 percent (strategy A) or same hat seen (strategy B)

    I see one flaw in your reasoning, which actually might push the game to a 50 percent winning. Here is the cycles I see:

    random + strategy A = 75 percent
    rrr/bbb + strategy A = 0 percent
    rrr/bbb + strategy B = 100 percent
    random + strategy B = 25 percent

    (Strategy B only wins in rrr/bbb, so it only wins in 2/8 cases.)

    The cycle will continues, pushing the game to a long-run 50 percent.

    If my analysis is correct, I offer one more intuitive explanation.

    The fact is each individual player can’t guess correctly more than 50 percent of the time. The gain to 75 percent is made only because all the wrong guesses are made together–on the rrr/bbb outcomes–where everyone is wrong. But in the end, each person does make four guesses, two that are right and two that are wrong.

    So if the host can disrupt coordinating wrong guesses, then the game settles to 50 percent.

    By Presh Talwalkar on Apr 16, 2008

  7. 75% is not the optimal solution. I can save n-1 people! If you wana know e-mail me fabricio003 (at) yahoo (dot) com (dot) br

    By Fabrício on Nov 10, 2008

  8. Interesting problem. I hadn’t run into this one before.

    When I first tried to solve it, I had misread the problem and thought that the players could guess in turn rather than simultaneously, and came up with a strategy for winning 87.5% of the time.

    **Spoiler space if you want to solve this one yourself**

    Call the players P1, P2, P3 (in guessing order). If P1 sees at least one blue hat he passes, otherwise he guesses blue for his own colour.

    If P1 passes, P2 then knows there’s at least one blue hat between his and P3. If P3 is red, he knows it’s him. If P3 is blue, he can pass and P3 will guess blue with certainty.

    This only loses in the RRR case – of course this strategy can be similarly adapted to any other losing case in the adapting-host scenario.

    It is not possible to win 100% in this case:
    Assume there’s a 100% winning strategy. Then the first player to guess must pass, because he cannot know his own colour. But if he must pass, then he cannot transmit any information to the other two. So P2 is in the same situation – he can’t guess either since he has no information about his own colour. Likewise for P3.

    By Cecil on Nov 28, 2008

  9. That is a neat variation and elegant solution. Thanks Cecil.

    By Presh Talwalkar on Dec 3, 2008

  10. >>No communication of any sort is allowed<<

    Is that so? Listening to your fellow players guess is communication. If that is allowed, how about listening to how long it takes to pass?

    Let’s say Alice is the official guesser, and Bob and Chris are the official passers. Bob passes at any time. If Alice’s hat is red, Chris immediately passes also. If Alice’s hat is blue, he waits 5 seconds, and then passes.

    Alice will then always know her hat color.

    By Curtis Autery on Aug 3, 2009

  11. I have a way to win 100% of the time.
    There always has to be at least two hats of the same color. When you walk in the room, the only person who passes is the one who sees two hats of the same color. Then either one of the 2 remaining players can correctly guess their own hat color by looking at the person’s hat who did not pass. If all 3 hats are the same color, then nobody passes – and any player can guess that their hat will be the color of the others’.

    By John on Aug 29, 2009

  12. Hey John, your response is meaningless. You’re referring to another hat game. The assumption in your first sentence is not the one of the problem described here. Better read an article before you tell us about a 100 % winning strategy!

    By Mussad on Sep 29, 2009

  13. I have a 100% winning strategy as well, but mine works, John.
    Before the game in this initial consulting session, I decide “I am the guesser, you two say pass regardless of the situation.” I determine who is pass 1 and who is pass 2. Then I say “If my had is red, pass 1 say ‘pass’ first. If my hat is blue, pass 2 say ‘pass’ first.” That way, Whichever says pass first, I know the color of my hat. Then I guess red or blue accordingly, and we are rich!

    By WG on Oct 7, 2009

Leave a Comment




Previous post:

Next post:

Other posts you may enjoy reading: