Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Next Greater Element Using Stack in DSA Typescript - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the stack as an empty array.

DSA Typescript
const stack: number[] = [1];
Drag options to blanks, or click blank then click option'
A{}
Bnull
C[]
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} which create an object, not an array.
Using null or 0 which are not arrays.
2fill in blank
medium

Complete the code to push the current index onto the stack.

DSA Typescript
stack.[1](i);
Drag options to blanks, or click blank then click option'
Apop
Bpush
Cshift
Dunshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop which removes elements instead of adding.
Using shift or unshift which operate on the start of the array.
3fill in blank
hard

Fix the error in the while loop condition to check if the stack is not empty.

DSA Typescript
while (stack.[1] > 0 && nums[stack[stack.length - 1]] < nums[i]) {
Drag options to blanks, or click blank then click option'
Alength()
Bsize
Ccount
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using size or count which are not valid for arrays.
Using length() with parentheses which causes an error.
4fill in blank
hard

Fill both blanks to correctly assign the next greater element and pop the stack.

DSA Typescript
result[stack[stack.length - 1]] = nums[[1]];
stack.[2]();
Drag options to blanks, or click blank then click option'
Ai
Bpop
Cpush
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using push instead of pop to remove elements.
Using length instead of i for indexing.
5fill in blank
hard

Fill all three blanks to complete the function that returns the next greater elements array.

DSA Typescript
function nextGreaterElement(nums: number[]): number[] {
  const result: number[] = new Array(nums.length).fill([1]);
  const stack: number[] = [];
  for (let [2] = 0; [2] < nums.length; [2]++) {
    while (stack.length > 0 && nums[stack[stack.length - 1]] < nums[[2]]) {
      result[stack.pop()!] = nums[[2]];
    }
    stack.push([2]);
  }
  return result;
}
Drag options to blanks, or click blank then click option'
A-1
Bi
Cindex
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of -1 as default fill value.
Using different variable names inconsistently.

Practice

(1/5)
1. What does the Next Greater Element problem using a stack find for each element in an array?
easy
A. The first smaller element to the left of each element
B. The first greater element to the right of each element
C. The last element in the array
D. The sum of all elements greater than the current element

Solution

  1. Step 1: Understand the problem goal

    The Next Greater Element problem aims to find, for each element, the first element to its right that is larger than it.
  2. Step 2: Identify the role of the stack

    A stack helps efficiently track elements for which we haven't found a greater element yet, allowing us to find the next greater element in one pass.
  3. Final Answer:

    The first greater element to the right of each element -> Option B
  4. Quick Check:

    Next Greater Element = first bigger right element [OK]
Hint: Look right for the first bigger number using a stack [OK]
Common Mistakes:
  • Confusing next greater with next smaller element
  • Searching to the left instead of right
  • Thinking it sums elements instead of finding one
2. Which of the following TypeScript code snippets correctly initializes an empty stack to use for Next Greater Element?
easy
A. var stack = null;
B. let stack = {};
C. const stack: number[] = [];
D. const stack = new Map();

Solution

  1. Step 1: Identify correct stack data structure

    For Next Greater Element, a stack is an array used with push/pop. So, an empty array of numbers is correct.
  2. Step 2: Check TypeScript syntax

    const stack: number[] = []; correctly declares an empty number array stack.
  3. Final Answer:

    const stack: number[] = []; -> Option C
  4. Quick Check:

    Stack = number array [] [OK]
Hint: Use number array [] for stack in TypeScript [OK]
Common Mistakes:
  • Using object {} instead of array for stack
  • Initializing stack as null
  • Using Map which is not a stack
3. What is the output of the Next Greater Element function for the input array [4, 5, 2, 25]?
medium
A. [5, 25, 25, -1]
B. [25, -1, 5, -1]
C. [5, -1, 25, -1]
D. [-1, 25, 25, -1]

Solution

  1. Step 1: Process each element to find next greater

    4's next greater is 5, 5's next greater is 25, 2's next greater is 25, 25 has none so -1.
  2. Step 2: Form the output array

    Output array is [5, 25, 25, -1].
  3. Final Answer:

    [5, 25, 25, -1] -> Option A
  4. Quick Check:

    Next Greater Element for [4,5,2,25] = [5,25,25,-1] [OK]
Hint: Check each element's right side for first bigger number [OK]
Common Mistakes:
  • Assigning -1 too early
  • Mixing up order of elements
  • Confusing next greater with next smaller
4. Identify the bug in this Next Greater Element TypeScript code snippet:
function nextGreater(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]] < arr[i]) {
      const index = stack.pop();
      result[index] = arr[i];
    }
    stack.push(i);
  }
  return result;
}
medium
A. Stack should store values, not indices
B. Result array initialized with zeros instead of -1
C. Loop should run backwards from end to start
D. Incorrect stack top access: should be stack[stack.length - 1]

Solution

  1. Step 1: Check stack top element access

    stack[stack.length] is undefined because array indices go from 0 to length-1. Correct is stack[stack.length - 1].
  2. Step 2: Confirm other parts are correct

    Result array initialization and loop direction are correct. Stack stores indices as expected.
  3. Final Answer:

    Incorrect stack top access: should be stack[stack.length - 1] -> Option D
  4. Quick Check:

    Stack top index = stack[stack.length - 1] [OK]
Hint: Stack top index is length-1, not length [OK]
Common Mistakes:
  • Using stack[stack.length] which is undefined
  • Initializing result with wrong default
  • Reversing loop unnecessarily
5. Given the array [13, 7, 6, 12], what is the Next Greater Element array using a stack approach?
hard
A. [ -1, 12, 12, -1 ]
B. [ -1, 13, 7, -1 ]
C. [ 7, 12, 13, -1 ]
D. [ 12, -1, -1, 13 ]

Solution

  1. Step 1: Find next greater for each element

    13 has no greater element to right, so -1. 7's next greater is 12. 6's next greater is 12. 12 has none, so -1.
  2. Step 2: Write the output array

    The result is [-1, 12, 12, -1].
  3. Final Answer:

    [-1, 12, 12, -1] -> Option A
  4. Quick Check:

    Next Greater Element for [13,7,6,12] = [-1,12,12,-1] [OK]
Hint: Check right side elements for first bigger number [OK]
Common Mistakes:
  • Assigning wrong next greater for 13
  • Mixing up indices and values
  • Confusing next greater with previous greater
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard