Goldman Sachs interview question

Medium leetcode style question was asked.