Bird
Raised Fist0
DSA Goprogramming~10 mins

Next Greater Element Using Stack in DSA Go - 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 slice.

DSA Go
stack := [1]
Drag options to blanks, or click blank then click option'
A[]int{}
Bmake(map[int]int)
Cnew([]int)
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Using a map instead of a slice for the stack.
Using nil which cannot be appended to.
2fill in blank
medium

Complete the code to pop the top element from the stack.

DSA Go
top := stack[len(stack)[1]]
stack = stack[:len(stack)-1]
Drag options to blanks, or click blank then click option'
A+1
B-2
C-1
D+2
Attempts:
3 left
💡 Hint
Common Mistakes
Using +1 causes index out of range error.
Using -2 accesses wrong element.
3fill in blank
hard

Fix the error in the condition to pop elements smaller than current element.

DSA Go
for len(stack) > 0 && nums[stack[len(stack)-1]] [1] nums[i] {
    top := stack[len(stack)-1]
    stack = stack[:len(stack)-1]
    result[top] = nums[i]
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > reverses the logic and breaks the algorithm.
Using == does not pop any elements.
4fill in blank
hard

Fill both blanks to append current index to stack and initialize result slice.

DSA Go
result := make([]int, len(nums))
for i := range result {
    result[i] = [1]
}

stack := [2]
Drag options to blanks, or click blank then click option'
A-1
B0
C[]int{}
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result with 0 causes confusion with valid elements.
Using nil for stack prevents appending.
5fill in blank
hard

Fill all three blanks to complete the Next Greater Element function.

DSA Go
func nextGreaterElement(nums []int) []int {
    result := make([]int, len(nums))
    for i := range result {
        result[i] = [1]
    }
    stack := [2]
    for i := 0; i < len(nums); i++ {
        for len(stack) > 0 && nums[stack[len(stack)-1]] [3] nums[i] {
            top := stack[len(stack)-1]
            stack = stack[:len(stack)-1]
            result[top] = nums[i]
        }
        stack = append(stack, i)
    }
    return result
}
Drag options to blanks, or click blank then click option'
A-1
B[]int{}
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > in condition breaks logic.
Initializing stack as nil causes append error.

Practice

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

Solution

  1. Step 1: Understand the problem goal

    The Next Greater Element problem asks to find for each element the first element to its right that is greater than it.
  2. Step 2: Recognize the stack usage

    A stack helps track elements for which we haven't found a greater element yet, efficiently finding the next greater element to the right.
  3. Final Answer:

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

    NGE = first greater element on right [OK]
Hint: Look right for first bigger number using stack [OK]
Common Mistakes:
  • Confusing next greater with next smaller element
  • Looking to the left instead of right
  • Thinking it sums elements instead of finding one
2. Which Go code snippet correctly initializes an empty stack to use for Next Greater Element logic?
easy
A. stack := 0
B. stack := make(map[int]int)
C. stack := new(int)
D. stack := []int{}

Solution

  1. Step 1: Identify stack data structure in Go

    Stacks can be implemented using slices in Go. An empty stack is an empty slice of ints: []int{}.
  2. Step 2: Check other options

    stack := make(map[int]int) is a map, not a stack. stack := new(int) is a pointer to int, not a stack. stack := 0 is an int variable, not a stack.
  3. Final Answer:

    stack := []int{} -> Option D
  4. Quick Check:

    Stack as slice = []int{} [OK]
Hint: Use empty int slice for stack in Go [OK]
Common Mistakes:
  • Using map instead of slice for stack
  • Using pointer instead of slice
  • Initializing stack as int variable
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. [5, -1, 25, -1]
C. [25, 25, -1, -1]
D. [-1, -1, -1, -1]

Solution

  1. Step 1: Trace the array elements with stack

    For 4, next greater is 5; for 5, next greater is 25; for 2, next greater is 25; for 25, no greater element, so -1.
  2. Step 2: Form the output array

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

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

    Next greater elements found correctly [OK]
Hint: Check each element's right side for bigger number [OK]
Common Mistakes:
  • Assigning -1 too early
  • Mixing up indices in output
  • Ignoring last element's -1
4. Given the following Go code snippet for Next Greater Element, what is the bug?
func nextGreater(arr []int) []int {
  stack := []int{}
  result := make([]int, len(arr))
  for i := len(arr) - 1; i >= 0; i-- {
    for len(stack) > 0 && stack[len(stack)-1] <= arr[i] {
      stack = stack[:len(stack)-1]
    }
    if len(stack) == 0 {
      result[i] = 0
    } else {
      result[i] = stack[len(stack)-1]
    }
    stack = append(stack, arr[i])
  }
  return result
}
medium
A. Stack is not initialized properly
B. Loop iterates in wrong direction
C. Result uses 0 instead of -1 for no greater element
D. Comparison operator should be >= instead of <=

Solution

  1. Step 1: Identify the no greater element marker

    The code assigns 0 when no greater element is found, but the standard is to use -1.
  2. Step 2: Check other parts

    Stack initialization and loop direction are correct. Comparison operator <= is correct for popping smaller or equal elements.
  3. Final Answer:

    Result uses 0 instead of -1 for no greater element -> Option C
  4. Quick Check:

    No greater element should be -1 [OK]
Hint: Use -1 for no next greater element, not 0 [OK]
Common Mistakes:
  • Returning 0 instead of -1 for no greater element
  • Changing loop direction incorrectly
  • Wrong comparison operator causing wrong pops
5. You have an array [13, 7, 6, 12]. Using the Next Greater Element stack approach, what is the output array?
hard
A. [ -1, -1, -1, -1 ]
B. [ -1, 12, 12, -1 ]
C. [ 12, 13, 7, -1 ]
D. [ 7, 6, 12, -1 ]

Solution

  1. Step 1: Find next greater for each element

    For 13, no greater element to right, so -1. For 7, next greater is 12. For 6, next greater is 12. For 12, no greater element, so -1.
  2. Step 2: Write output array

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

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

    Check right side for bigger numbers [OK]
Hint: Check each element's right side for bigger number [OK]
Common Mistakes:
  • Assigning wrong next greater for 7 and 6
  • Confusing output with input order
  • Using 0 instead of -1 for no greater element
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