TikTok interview question

a hard level leetcode programming problem