Google interview question

two leetcode mediums, not that hard