Google interview question

I was asked to solve a leetcode question.