Challenge - 5 Problems
Next Greater Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Next Greater Element for a given array
What is the output of the following code that finds the next greater element for each element in the array using a stack?
DSA Python
def next_greater_element(arr): stack = [] result = [-1] * len(arr) for i in range(len(arr)): while stack and arr[stack[-1]] < arr[i]: result[stack.pop()] = arr[i] stack.append(i) return result print(next_greater_element([4, 5, 2, 25]))
Attempts:
2 left
💡 Hint
Think about how the stack helps track indices of elements waiting for a greater element.
✗ Incorrect
The stack stores indices of elements for which we haven't found a greater element yet. When a greater element is found, it updates the result for those indices.
🧠 Conceptual
intermediate1:30remaining
Understanding stack usage in Next Greater Element
Why do we use a stack to solve the Next Greater Element problem efficiently?
Attempts:
2 left
💡 Hint
Think about how the stack helps avoid repeated comparisons.
✗ Incorrect
Stack stores indices of elements waiting for their next greater element. When a greater element appears, it updates all smaller elements on the stack efficiently.
🔧 Debug
advanced2:00remaining
Identify the error in Next Greater Element code
What error does the following code produce when run, and why?
```python
def next_greater_element(arr):
stack = []
result = [-1] * len(arr)
for i in range(len(arr)):
while stack and arr[i] > arr[stack[-1]]:
result[stack.pop()] = arr[i]
stack.append(i)
return result
print(next_greater_element([13, 7, 6, 12]))
```
DSA Python
def next_greater_element(arr): stack = [] result = [-1] * len(arr) for i in range(len(arr)): while stack and arr[i] > arr[stack[-1]]: result[stack.pop()] = arr[i] stack.append(i) return result print(next_greater_element([13, 7, 6, 12]))
Attempts:
2 left
💡 Hint
Trace the stack and result updates carefully for each element.
✗ Incorrect
The code correctly finds the next greater element for each index:
- 13 has no greater element after it, so -1
- 7's next greater is 12
- 6's next greater is 12
- 12 has no greater element after it, so -1
❓ Predict Output
advanced2:00remaining
Output of Next Greater Element for circular array
What is the output of the following code that finds the next greater element in a circular array using a stack?
DSA Python
def next_greater_circular(arr): n = len(arr) result = [-1] * n stack = [] for i in range(2 * n): while stack and arr[stack[-1]] < arr[i % n]: result[stack.pop()] = arr[i % n] if i < n: stack.append(i) return result print(next_greater_circular([1, 2, 1]))
Attempts:
2 left
💡 Hint
Remember the array is treated as circular, so elements after the end wrap around.
✗ Incorrect
For circular arrays, we simulate two passes. For [1,2,1]:
- Next greater for 1 is 2
- Next greater for 2 is none (-1)
- Next greater for last 1 is 2
🚀 Application
expert1:30remaining
Number of elements with no next greater element
Given the array [11, 13, 21, 3], how many elements have no next greater element to their right?
Attempts:
2 left
💡 Hint
Find the next greater element for each and count how many are -1.
✗ Incorrect
Next greater elements are:
- 11 -> 13
- 13 -> 21
- 21 -> none (-1)
- 3 -> none (-1)
So two elements have no next greater element to their right.