EndoVantage interview question

Optimize code to speed it up