Google interview question

Leetcode hard question on their platform