Meta interview question

leetcode qtn, med - hard version