Google Interview Question

I was asked to solve a leetcode question.