Pregunta de entrevista de Bitscopic

Typical coding question that has multiple ways to be solved. They ask you to optimize and explain your thinking etc.