Pergunta de entrevista da empresa Wahed Invest

How would I optimize my code solution