Google Interview Question

How can you optimize this for Big-O time?