Goldman Sachs interview question

It was leetcode easy to medium question.