Google interview question

something close to leetcode 300