Goldman Sachs Interview Question

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