Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat 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) }
Attempts:
2 left
💡 Hint
Remember that append adds to the end and slicing with [:len(stack)-1] removes the last element.
✗ Incorrect
The stack after pushing 10, 20, 30 is [10 20 30]. After popping once, the last element (30) is removed, leaving [10 20].
🧠 Conceptual
intermediateHow 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?
Attempts:
2 left
💡 Hint
Think about what happens when slicing an empty slice with negative indices.
✗ Incorrect
Trying to slice beyond the bounds of an empty slice causes a runtime panic in Go.
🔧 Debug
advancedIdentify 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) }
Attempts:
2 left
💡 Hint
Remember that slices are passed by value, so changes inside the function do not affect the original slice unless returned.
✗ Incorrect
In Go, slices are passed by value. The append returns a new slice which must be assigned back to the caller. Without returning the updated slice, the original stack remains unchanged.
🚀 Application
advancedHow 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?
Attempts:
2 left
💡 Hint
Think about how to prevent adding elements when the stack is full.
✗ Incorrect
To prevent overflow, check the current length against maxSize before pushing. If full, reject the push.
❓ Predict Output
expertWhat 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) }
Attempts:
2 left
💡 Hint
Trace each push and pop step carefully, noting the stack state after each operation.
✗ Incorrect
Push 1,2,3 → [1 2 3]. Pop removes 3 → [1 2]. Push 4 → [1 2 4]. Pop removes 4 → [1 2]. Push 5 → [1 2 5].
