The solution seems overly complicated.
Because then on average only 50% would be saved.
Interesting problem. Good way to check if the programmer is familiar with XOR operation. But why limit the number of colors to 2? Let's say the number of programmers is N and the number of colors of the hats is K. How many programmers will survive in this case? ;)
Fog Creek Home