Goldman Sachs Interview Question

Stack realted question - next greater element