Goldman Sachs interview question

DSA question based on stack