TikTok Interview Question

Leetcode 22 and follow up questions.