Goldman Sachs Interview Question

Common algorithm question.