Google interview question

Leetcode