Apple Interview Question

This piece of code is very slow. Fix it.