TikTok interview question

2 questions: both Leetcode hard questions