Katkat

Maximize correct guesses in a hat line puzzle

SnapchatSoftware Engineer · Onsite

Last updated

This problem tests your ability to devise a deterministic strategy for a group of people to maximize correct guesses about their own attributes based on observed information from others.

The Hat Line Puzzle

100 people are standing in a straight line, facing the same direction. Each person is wearing a hat that is either red or blue. Each person can see all the hats of the people standing in front of them in the line, but they cannot see their own hat or the hats of the people behind them.

Starting from the back of the line (the person who can see 99 hats), each person is asked to call out 'Red' or 'Blue'. If they guess their own hat color correctly, they survive. If they guess incorrectly, they are eliminated.

Before the line begins, the group can agree on a strategy. Each person hears the guesses made by the people behind them, but they do not know if those people were correct or incorrect unless the rules allow for that information (assume they only hear the guess, not the result).

Design a strategy that ensures at least 99 out of the 100 people survive regardless of the hat distribution. Provide the logic for the first person's guess and how subsequent people derive their own color from that information.

Constraints

  • The guessing must happen sequentially from the back of the line to the front.
  • Each person only hears the guesses of those who went before them.
  • You must provide a strategy that guarantees 99 survivors.

Example

If person 100 sees an even number of red hats in front of them, they might guess 'Red'. If person 99 sees an odd number of red hats, they can deduce their own color based on what they observe versus what person 100 observed.

Hints

Hint 1

Consider assigning a numeric value to each color, such as Red = 0 and Blue = 1.

Hint 2

The first person can use their guess to communicate the parity (even or odd) of the total number of red hats they see in front of them.

Hint 3

Each subsequent person can maintain a running count of the hat colors they have heard and what they currently see to isolate their own color.

Follow-up questions

  • How does the strategy change if there are three possible hat colors (Red, Blue, Green) instead of two?
  • If the first person is allowed to guess incorrectly to save others, could you guarantee all 100 people survive?
  • What is the mathematical foundation (e.g., modular arithmetic) that makes this communication strategy possible?