← Apple Interview InsightsSoftware Engineer

Apple·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Apple SWE interview with a classic logic puzzle that sounds approachable until you actually have to explain the parity encoding part under pressure.

Questions Asked (1)

Q1

A line of dwarves each wear a black or white hat. Each dwarf can see all hats in front but not their own. Starting from the back, each dwarf announces their own hat color, and everyone hears all prior announcements. They can agree on a strategy beforehand. What strategy guarantees the most correct guesses?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew the answer involved parity and got there eventually, but my explanation was a mess the first time through.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that the optimal strategy uses the first dwarf's announcement as a parity bit for the number of black hats ahead, sacrificing his own correctness to guarantee all others are correct. Then describe how each subsequent dwarf deduces their hat color by comparing the parity of hats they see with the announced parity and previous guesses.

Pro tip: Emphasize that this strategy guarantees at most one wrong guess, which is optimal because the first dwarf has no information about his own hat. Mention that the parity can be computed using XOR or modulo 2, and that the strategy works for any number of dwarves.

1. Understand the problem constraints

Identify that each dwarf sees all hats in front, hears all prior announcements, and can agree on a strategy beforehand. The goal is to maximize the number of correct guesses.

2. Design the parity strategy

Agree that the first dwarf (at the back) will announce 'black' if he sees an odd number of black hats ahead, and 'white' if even. This encodes the parity of black hats in the line ahead.

3. Deduce each subsequent hat

Each following dwarf counts the black hats he sees ahead, combines it with the parity announced by the first dwarf and the announcements of dwarves behind him, to determine his own hat color.

4. Verify correctness and optimality

Show that every dwarf except possibly the first guesses correctly, and argue that no strategy can guarantee more than n-1 correct guesses because the first dwarf has no information about his own hat.

Key Points to Mention

  • Parity (odd/even) of black hats as the key information
  • First dwarf's guess is sacrificial but conveys necessary information
  • Each subsequent dwarf uses prior announcements to update their count
  • XOR or modulo 2 arithmetic for parity calculation
  • Optimality: at most one wrong guess is unavoidable
  • Strategy works for any number of dwarves and any distribution of hats

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.