TikTok Interview Question

leetcode medium( 4 and 32)