Google interview question

Leetcode medium level question to be solved in 30 minutes