TikTok Interview Question

A leetcode problem with medium difficulties.