Pergunta de entrevista da empresa Microsoft

There was a hard puzzle given to solve.