Google interview question

Medium leetcode question and its complexity analysis.