Klaviyo interview question

How to optimize this code