Intuit interview question

I was asked to optimize the algorithm I gave.