Goldman Sachs interview question

explain me the approach with an example very clearly before jumping into coding part.