Google Interview Question

How would you optimize this solution