Amazon interview question

Stack question based on next greater element.