TikTok Interview Question

It is a leetcode question.