Anonymous interview question

How to optimize given codes