Pregunta de entrevista de RBC

could you write codes for this problem?