TikTok interview question

A leetcode problem with medium difficulties.