Pergunta de entrevista da empresa Bloomberg

gave a puzzle to solve and asked to programme it