Pregunta de entrevista de Deem

Very complex augorithm question