Traveloka interview question

How to optimize this solution?