Google interview question

Leetcode question