Google interview question

Technical: the solution, walking through it and expected runtime