Clear Street interview question

Leetcode style problem, medium to hard difficulty