Intuit Interview Question

I was asked to optimize the algorithm I gave.