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 TypeScript code that finds the next greater element for each item in the array?
DSA Typescript
function nextGreaterElement(arr: number[]): number[] {
const stack: number[] = [];
const result: number[] = 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([4, 5, 2, 25]));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 we haven't found a greater element yet. When a bigger element is found, it updates the result for all smaller elements on the stack.
❓ Predict Output
intermediateNext Greater Element output with repeated elements
What is the output of this TypeScript code for the array with repeated elements?
DSA Typescript
function nextGreaterElement(arr: number[]): number[] {
const stack: number[] = [];
const result: number[] = 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, 2, 1, 3]));Attempts:
2 left
💡 Hint
Remember that equal elements do not count as greater.
✗ Incorrect
The next greater element for both 2's and 1 is 3. The last element 3 has no greater element, so -1.
🔧 Debug
advancedIdentify the error in Next Greater Element implementation
What error will this TypeScript code produce when run?
DSA Typescript
function nextGreaterElement(arr: number[]): number[] {
const stack: number[] = [];
const result: number[] = 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, 3, 2]));Attempts:
2 left
💡 Hint
Check the loop boundary and array access.
✗ Incorrect
The loop runs with i <= arr.length, so when i equals arr.length, arr[i] is undefined. Accessing arr[i] causes a TypeError.
🧠 Conceptual
advancedTime complexity of Next Greater Element using stack
What is the time complexity of the Next Greater Element algorithm using a stack for an array of size n?
Attempts:
2 left
💡 Hint
Think about how many times each element is processed.
✗ Incorrect
Each element is pushed onto the stack once and popped once, so total operations are proportional to n, making it O(n).
🚀 Application
expertNext Greater Element for circular array
Given a circular array, where the next element after the last is the first element, what is the output of this TypeScript code?
DSA Typescript
function nextGreaterElementCircular(arr: number[]): number[] {
const n = arr.length;
const stack: number[] = [];
const result: number[] = new Array(n).fill(-1);
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([5, 3, 8, -2, 7]));Attempts:
2 left
💡 Hint
Remember the array is circular, so elements after the end wrap to the start.
✗ Incorrect
The code checks twice the length to simulate circular behavior. For each element, it finds the next greater element considering wrap-around.
