Pregunta de entrevista de Amazon

Stack question based on next greater element.