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/pop sequence?
Consider a stack implemented using a slice in Go. What will be printed after these operations?
DSA Go
package main import "fmt" func main() { stack := []int{} stack = append(stack, 10) stack = append(stack, 20) stack = append(stack, 30) fmt.Print(stack[len(stack)-1], " -> ") stack = stack[:len(stack)-1] fmt.Print(stack[len(stack)-1], " -> ") stack = stack[:len(stack)-1] fmt.Print(stack[len(stack)-1], " -> null") }
Attempts:
2 left
💡 Hint
Remember, stack follows Last In First Out (LIFO). The last pushed item is popped first.
✗ Incorrect
The stack after pushes is [10, 20, 30]. The first pop removes 30, printing it. Next pop removes 20, then 10 remains. So output is 30 -> 20 -> 10 -> null.
🧠 Conceptual
intermediateHow many items remain in the stack after these operations?
A stack starts empty. We push 5 items, then pop 3 items. How many items remain in the stack?
Attempts:
2 left
💡 Hint
Each pop removes one item from the stack.
✗ Incorrect
Starting with 0, push 5 items → stack size 5. Pop 3 items → stack size 2 remain.
🔧 Debug
advancedWhat error does this Go stack pop code cause?
What error will this code produce when run?
DSA Go
package main import "fmt" func main() { stack := []int{} stack = append(stack, 1) stack = stack[:len(stack)-1] stack = stack[:len(stack)-1] fmt.Println(stack) }
Attempts:
2 left
💡 Hint
Check what happens when you try to slice beyond zero length.
✗ Incorrect
After popping once, stack length is 0. Trying to pop again slices beyond zero length causing runtime panic: slice bounds out of range.
🚀 Application
advancedWhat is the final stack state after these operations?
Starting with an empty stack, perform these operations in order: push 5, push 10, pop, push 15, pop, push 20. What is the stack content from top to bottom?
Attempts:
2 left
💡 Hint
Track each push and pop carefully, remembering LIFO order.
✗ Incorrect
Push 5 → [5]
Push 10 → [5,10]
Pop → removes 10 → [5]
Push 15 → [5,15]
Pop → removes 15 → [5]
Push 20 → [5,20]
Top is 20, then 5.
❓ Predict Output
expertWhat is printed by this Go stack reversal code?
This code reverses a stack using another stack. What is the printed output?
DSA Go
package main import "fmt" func main() { stack := []int{1, 2, 3, 4} reversed := []int{} for len(stack) > 0 { top := stack[len(stack)-1] stack = stack[:len(stack)-1] reversed = append(reversed, top) } for i := len(reversed) - 1; i >= 0; i-- { fmt.Print(reversed[i], " -> ") } fmt.Print("null") }
Attempts:
2 left
💡 Hint
The code pops all items from stack to reversed, then prints reversed in reverse order.
✗ Incorrect
Original stack: [1,2,3,4]
After moving to reversed: [4,3,2,1]
Printing reversed in reverse order prints 1 -> 2 -> 3 -> 4 -> null.
