Bird
Raised Fist0
DSA Goprogramming~20 mins

Stack Implementation Using Array in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go stack push and pop sequence?
Consider a stack implemented using an array with push and pop operations. What will be printed after the following operations?
DSA Go
package main
import "fmt"
func main() {
    stack := []int{}
    stack = append(stack, 10)  // push 10
    stack = append(stack, 20)  // push 20
    stack = append(stack, 30)  // push 30
    fmt.Println(stack)
    stack = stack[:len(stack)-1]  // pop
    fmt.Println(stack)
}
A
[10 20 30]
[10 20]
B
[10 20]
[10]
C
[10 20 30]
[10 20 30]
D
[30 20 10]
[20 10]
Attempts:
2 left
💡 Hint
Remember that append adds to the end and slicing with [:len(stack)-1] removes the last element.
🧠 Conceptual
intermediate
1:30remaining
How does a stack implemented with an array behave when popping from an empty stack?
What happens if you try to pop an element from an empty stack implemented using an array in Go?
AIt throws a compile-time error.
BIt returns a zero value without error.
CIt automatically resizes the array to add a default element.
DIt causes a runtime panic due to slicing beyond bounds.
Attempts:
2 left
💡 Hint
Think about what happens when slicing an empty slice with negative indices.
🔧 Debug
advanced
2:00remaining
Identify the bug in this stack push function using array
What is wrong with this push function for a stack implemented using an array in Go?
DSA Go
func push(stack []int, val int) {
    stack = append(stack, val)
}
AThe function should remove the last element before appending.
BThe append function is used incorrectly; it should be stack.append(val).
CThe function does not return the updated stack, so changes are lost.
DThe function should use a pointer receiver to modify the stack.
Attempts:
2 left
💡 Hint
Remember that slices are passed by value, so changes inside the function do not affect the original slice unless returned.
🚀 Application
advanced
2:30remaining
How to implement a stack with fixed maximum size using an array in Go?
You want to implement a stack with a fixed maximum size using an array in Go. Which approach correctly prevents pushing beyond the maximum size?
ACheck if len(stack) == maxSize before append; if true, do not append and return error.
BUse append without checks; Go arrays automatically resize.
CUse a slice with capacity maxSize and append without limit; Go will panic if exceeded.
DPre-allocate an array of maxSize and overwrite elements without tracking size.
Attempts:
2 left
💡 Hint
Think about how to prevent adding elements when the stack is full.
❓ Predict Output
expert
3:00remaining
What is the output after multiple push and pop operations on this Go stack?
Given the following Go code implementing stack operations, what is the final printed stack?
DSA Go
package main
import "fmt"
func main() {
    stack := []int{}
    stack = append(stack, 1)
    stack = append(stack, 2)
    stack = append(stack, 3)
    stack = stack[:len(stack)-1]  // pop 3
    stack = append(stack, 4)
    stack = stack[:len(stack)-1]  // pop 4
    stack = append(stack, 5)
    fmt.Println(stack)
}
A[1 5]
B[1 2 5]
C[1 2 4 5]
D[1 2 3 5]
Attempts:
2 left
💡 Hint
Trace each push and pop step carefully, noting the stack state after each operation.

Practice

(1/5)
1. What is the main purpose of using an array to implement a stack?
easy
A. To sort elements automatically
B. To store elements in a first-in, first-out (FIFO) order
C. To store elements in a last-in, first-out (LIFO) order
D. To allow random access to elements

Solution

  1. Step 1: Understand stack behavior

    A stack stores elements so the last added is the first removed, called LIFO.
  2. Step 2: Role of array in stack

    An array holds elements in order, supporting push/pop at one end to maintain LIFO.
  3. Final Answer:

    To store elements in a last-in, first-out (LIFO) order -> Option C
  4. Quick Check:

    Stack = LIFO [OK]
Hint: Stack means last-in, first-out order [OK]
Common Mistakes:
  • Confusing stack with queue (FIFO)
  • Thinking array sorts elements automatically
  • Assuming random access is main stack feature
2. Which of the following is the correct way to declare a stack using an array in Go?
easy
A. stack := []int{}
B. stack := array int
C. var stack [10]int
D. var stack int[]

Solution

  1. Step 1: Understand Go slice declaration

    In Go, slices are dynamic arrays declared as []type{}, e.g., []int{}.
  2. Step 2: Check options for correct syntax

    stack := []int{} uses correct slice literal syntax to create an empty int slice.
  3. Final Answer:

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

    Go slice syntax = []type{} [OK]
Hint: Use slice literal []int{} to declare empty stack [OK]
Common Mistakes:
  • Using array keyword incorrectly
  • Wrong order of type and brackets
  • Declaring fixed size array instead of slice
3. What will be the output of the following Go code snippet implementing a stack push and pop?
package main
import "fmt"
func main() {
  stack := []int{}
  stack = append(stack, 10)
  stack = append(stack, 20)
  top := stack[len(stack)-1]
  stack = stack[:len(stack)-1]
  fmt.Println(top)
  fmt.Println(stack)
}
medium
A. 10 []
B. 20 [10]
C. 20 [10 20]
D. 10 [20]

Solution

  1. Step 1: Trace push operations

    stack starts empty, append 10 -> [10], append 20 -> [10, 20]
  2. Step 2: Trace pop operation

    top = last element = 20, then remove last element -> stack becomes [10]
  3. Final Answer:

    20 [10] -> Option B
  4. Quick Check:

    Pop returns 20, stack left with 10 [OK]
Hint: Pop returns last pushed element, stack shrinks [OK]
Common Mistakes:
  • Printing stack before pop
  • Confusing first element with last
  • Not slicing stack correctly after pop
4. Identify the error in this Go code snippet for stack push operation:
package main
func main() {
  var stack []int
  stack[len(stack)] = 5
}
medium
A. Index out of range error
B. Syntax error: missing import
C. No error, code runs fine
D. Type mismatch error

Solution

  1. Step 1: Understand slice indexing

    Accessing stack[len(stack)] tries to assign beyond current slice length, causing error.
  2. Step 2: Correct push uses append

    To add element, use append(stack, 5) instead of direct index assignment.
  3. Final Answer:

    Index out of range error -> Option A
  4. Quick Check:

    Assigning at len(slice) causes index error [OK]
Hint: Use append to add elements, not direct index [OK]
Common Mistakes:
  • Trying to assign at slice length index
  • Forgetting to import fmt (not needed here)
  • Assuming slice auto-expands on index assignment
5. Given a stack implemented using an array in Go, how would you modify the pop operation to safely handle popping from an empty stack without causing a runtime error?
hard
A. Replace the stack with a map to avoid empty pop errors
B. Always pop without checking; Go handles empty slices gracefully
C. Use a for loop to pop all elements at once
D. Check if stack length is zero before popping and return an error or zero value

Solution

  1. Step 1: Understand pop risk on empty stack

    Popping from empty slice causes index out of range runtime error.
  2. Step 2: Add length check before pop

    Check if len(stack) == 0; if yes, return error or zero value to avoid crash.
  3. Final Answer:

    Check if stack length is zero before popping and return an error or zero value -> Option D
  4. Quick Check:

    Empty stack pop needs length check [OK]
Hint: Always check stack length before pop to avoid errors [OK]
Common Mistakes:
  • Ignoring empty stack check causing runtime panic
  • Using map instead of array unnecessarily
  • Trying to pop all elements in a loop without condition
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