TikTok interview question

leetcode medium( 4 and 32)