SG Digital Interview Question

How would you optimize this code?