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 Next Greater Element array for the input array [4, 5, 2, 25] using a stack-based approach?
DSA C
int arr[] = {4, 5, 2, 25}; int n = 4; // Next Greater Element logic using stack // Output format: [NGE for each element in order]
Attempts:
2 left
💡 Hint
Remember, the next greater element for each element is the first element to the right that is greater than it.
✗ Incorrect
For 4, the next greater element is 5; for 5, it is 25; for 2, it is 25; for 25, there is no greater element to the right, so -1.
❓ Predict Output
intermediate2:00remaining
Next Greater Element output for a descending array
What is the output of the Next Greater Element array for the input array [9, 7, 5, 3, 1] using a stack?
DSA C
int arr[] = {9, 7, 5, 3, 1}; int n = 5; // Next Greater Element logic using stack // Output format: [NGE for each element in order]
Attempts:
2 left
💡 Hint
In a strictly descending array, no element has a greater element to its right.
✗ Incorrect
Since the array is strictly descending, no element has a next greater element, so all are -1.
🔧 Debug
advanced2:30remaining
Identify the error in Next Greater Element implementation
What error will this code produce when finding the Next Greater Element using a stack?
DSA C
int arr[] = {2, 7, 3, 5, 4, 6, 8}; int n = 7; int stack[7]; int top = -1; int result[7]; for (int i = 0; i < n; i++) result[i] = -1; for (int i = 0; i < n; i++) { while (top >= 0 && arr[i] > arr[stack[top]]) { result[stack[top]] = arr[i]; top--; } stack[++top] = i; } // print result array
Attempts:
2 left
💡 Hint
Check if the stack is used correctly and indices are managed properly.
✗ Incorrect
The code correctly uses a stack to track indices and updates the result array with the next greater elements. No errors occur.
🧠 Conceptual
advanced1:30remaining
Time complexity of Next Greater Element using stack
What is the time complexity of finding the Next Greater Element for an array of size n using a stack?
Attempts:
2 left
💡 Hint
Each element is pushed and popped at most once from the stack.
✗ Incorrect
Each element is pushed onto the stack once and popped once, so total operations are proportional to n, making it O(n).
🚀 Application
expert3:00remaining
Next Greater Element for circular array
Given a circular array [1, 2, 1], what is the Next Greater Element array using a stack approach that considers the array circular (elements after the last wrap to the start)?
DSA C
int arr[] = {1, 2, 1}; int n = 3; // Circular Next Greater Element logic using stack // Output format: [NGE for each element in order]
Attempts:
2 left
💡 Hint
For circular arrays, consider iterating twice the length and use modulo for indices.
✗ Incorrect
For element 1 at index 0, next greater is 2; for 2 at index 1, no greater element; for 1 at index 2, next greater is 2 (from index 1).
