Anonymous Interview Question

How to optimize given codes