Pregunta de entrevista de Bending Spoons

Logical reasoning test, programming exercise.