TikTok Interview Question

a hard level leetcode programming problem