TikTok interview question

Q: Please solve this leetcode question.