Google interview question

Leetcode question. Doing tagged helps