Amazon Interview Question

Stack question based on next greater element.