Recall & Review
beginner
What is the Next Greater Element (NGE) for an element in an array?
The Next Greater Element for an element is the first element to the right of it in the array that is greater than it. If no such element exists, the NGE is -1.
Click to reveal answer
intermediate
Why do we use a stack to solve the Next Greater Element problem efficiently?
A stack helps keep track of elements for which we haven't found the NGE yet. It allows us to process elements in a way that avoids repeated scanning, reducing time complexity from O(n²) to O(n).
Click to reveal answer
intermediate
In the NGE algorithm using a stack, what do we do when the current element is greater than the element at the top of the stack?
We pop elements from the stack until the top element is greater than the current element or the stack is empty. For each popped element, the current element is its Next Greater Element.
Click to reveal answer
beginner
What is the time complexity of the Next Greater Element algorithm using a stack?
The time complexity is O(n) because each element is pushed and popped at most once from the stack.
Click to reveal answer
beginner
What will be the output for the array [4, 5, 2, 25] using the Next Greater Element algorithm?
The output will be [5, 25, 25, -1]. Explanation: 4's NGE is 5, 5's NGE is 25, 2's NGE is 25, and 25 has no greater element to its right.
Click to reveal answer
What does the stack store in the Next Greater Element algorithm?
✗ Incorrect
The stack stores elements (or their indices) for which we haven't found the Next Greater Element yet.
If the current element is smaller than the top of the stack, what should we do?
✗ Incorrect
If the current element is smaller, we push it onto the stack because it might have a greater element later.
What value is assigned if no Next Greater Element exists for an element?
✗ Incorrect
We assign -1 to indicate no greater element exists to the right.
What is the worst-case time complexity of the naive approach to find NGE without a stack?
✗ Incorrect
The naive approach checks each element's right side, leading to O(n²) time.
Which data structure is best suited for solving the Next Greater Element problem efficiently?
✗ Incorrect
A stack allows efficient tracking of elements waiting for their NGE.
Explain how the stack is used step-by-step in the Next Greater Element algorithm.
Think about how the stack helps remember elements waiting for their next bigger number.
You got /5 concepts.
Describe the difference in time complexity between the naive and stack-based Next Greater Element solutions.
Focus on how many times each element is processed.
You got /4 concepts.
