Google Interview Question

I didn't pay attention to time/space complexity of the code.