Pergunta de entrevista da empresa RBC

could you write codes for this problem?