Pregunta de entrevista de Wahed Invest

How would I optimize my code solution