Challenge - 5 Problems
Next Greater Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Next Greater Element for a simple array
What is the output of the following JavaScript code that finds the next greater element for each item in the array?
DSA Javascript
function nextGreaterElement(arr) {
const stack = [];
const result = new Array(arr.length).fill(-1);
for (let i = 0; i < arr.length; i++) {
while (stack.length && arr[stack[stack.length - 1]] < arr[i]) {
const idx = stack.pop();
result[idx] = arr[i];
}
stack.push(i);
}
return result;
}
console.log(nextGreaterElement([2, 1, 3]));Attempts:
2 left
💡 Hint
Think about how the stack helps track indices waiting for a bigger number.
✗ Incorrect
The code uses a stack to keep indices of elements for which the next greater element is not found yet. When a bigger element is found, it updates the result for all smaller elements on the stack.
❓ Predict Output
intermediateNext Greater Element output for descending array
What will be the output of the next greater element function when the input array is strictly descending?
DSA Javascript
function nextGreaterElement(arr) {
const stack = [];
const result = new Array(arr.length).fill(-1);
for (let i = 0; i < arr.length; i++) {
while (stack.length && arr[stack[stack.length - 1]] < arr[i]) {
const idx = stack.pop();
result[idx] = arr[i];
}
stack.push(i);
}
return result;
}
console.log(nextGreaterElement([5, 4, 3, 2, 1]));Attempts:
2 left
💡 Hint
In a descending array, no element has a greater element to its right.
✗ Incorrect
Since the array is strictly descending, no element to the right is greater than the current element, so all results remain -1.
🔧 Debug
advancedIdentify the error in Next Greater Element implementation
What error will the following code produce when run, and why?
DSA Javascript
function nextGreaterElement(arr) {
const stack = [];
const result = new Array(arr.length).fill(-1);
for (let i = 0; i <= arr.length; i++) {
while (stack.length && arr[stack[stack.length - 1]] < arr[i]) {
const idx = stack.pop();
result[idx] = arr[i];
}
stack.push(i);
}
return result;
}
console.log(nextGreaterElement([1, 2, 3]));Attempts:
2 left
💡 Hint
Check the loop boundary and array access inside the loop.
✗ Incorrect
The loop runs one step too far (i <= arr.length), causing arr[i] to be undefined on the last iteration, leading to a TypeError when comparing.
🧠 Conceptual
advancedStack usage in Next Greater Element algorithm
Why does the Next Greater Element algorithm use a stack to store indices instead of values directly?
Attempts:
2 left
💡 Hint
Think about how the result array is updated.
✗ Incorrect
Storing indices allows the algorithm to update the result array at the exact position corresponding to the element whose next greater element is found.
❓ Predict Output
expertNext Greater Element output for circular array
What is the output of the following code that finds the next greater element in a circular array?
DSA Javascript
function nextGreaterElementCircular(arr) {
const n = arr.length;
const result = new Array(n).fill(-1);
const stack = [];
for (let i = 0; i < 2 * n; i++) {
const current = arr[i % n];
while (stack.length && arr[stack[stack.length - 1]] < current) {
const idx = stack.pop();
result[idx] = current;
}
if (i < n) stack.push(i);
}
return result;
}
console.log(nextGreaterElementCircular([1, 2, 1]));Attempts:
2 left
💡 Hint
Remember the array is circular, so elements after the end wrap around to the start.
✗ Incorrect
The circular approach allows elements at the end to find next greater elements at the start. For example, the last 1 finds 2 as next greater element from the start.
