0
0
DSA Pythonprogramming~20 mins

Next Greater Element Using Stack in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next Greater Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]))
A[5, 25, 25, -1]
B[-1, -1, -1, -1]
C[25, 25, -1, -1]
D[5, 2, 25, -1]
Attempts:
2 left
💡 Hint
Think about how the stack helps track indices of elements waiting for a greater element.
🧠 Conceptual
intermediate
1:30remaining
Understanding stack usage in Next Greater Element
Why do we use a stack to solve the Next Greater Element problem efficiently?
ABecause stack duplicates the array elements to compare each with every other element.
BBecause stack sorts the array automatically to find the next greater element.
CBecause stack stores all elements in sorted order to find the maximum element.
DBecause stack helps keep track of elements for which we haven't found the next greater element yet, allowing O(n) time complexity.
Attempts:
2 left
💡 Hint
Think about how the stack helps avoid repeated comparisons.
🔧 Debug
advanced
2: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]))
ARaises IndexError due to popping from empty stack
BOutput: [ -1, 12, 12, -1 ]
COutput: [ 7, 6, 12, -1 ]
DOutput: [ 12, -1, -1, -1 ]
Attempts:
2 left
💡 Hint
Trace the stack and result updates carefully for each element.
Predict Output
advanced
2: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]))
A[1, 2, 1]
B[-1, -1, -1]
C[2, -1, 2]
D[2, 1, 2]
Attempts:
2 left
💡 Hint
Remember the array is treated as circular, so elements after the end wrap around.
🚀 Application
expert
1: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?
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Find the next greater element for each and count how many are -1.