Goldman Sachs interview question

Common algorithm question.