Variphy interview question

Leetcode question followed by optimization questions.