GlobalLogic Interview Question

Prisoners hat problem?