Question d’entretien chez TikTok

Leetcode question. Then try to improve time complexity.