Challenge - 5 Problems
Stack Pop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after popping from the stack?
Consider a stack implemented as a slice in Go. What will be the printed stack after popping one element?
DSA Go
package main import "fmt" func main() { stack := []int{10, 20, 30, 40} stack = stack[:len(stack)-1] // pop operation fmt.Println(stack) }
Attempts:
2 left
💡 Hint
Remember that popping removes the last element from the stack.
✗ Incorrect
The pop operation removes the last element (40) from the stack, leaving [10 20 30].
❓ Predict Output
intermediateWhat happens if you pop from an empty stack?
What will happen when the following Go code runs?
DSA Go
package main import "fmt" func main() { stack := []int{} stack = stack[:len(stack)-1] // pop operation fmt.Println(stack) }
Attempts:
2 left
💡 Hint
Think about what happens when you try to slice with a negative index.
✗ Incorrect
Since the stack is empty, len(stack) is 0, so len(stack)-1 is -1, causing a runtime panic due to invalid slice bounds.
🔧 Debug
advancedIdentify the error in this pop implementation
What error will this Go code produce when popping from the stack?
DSA Go
package main import "fmt" func pop(stack []int) ([]int, int) { val := stack[len(stack)-1] stack = stack[:len(stack)-1] return stack, val } func main() { stack := []int{1, 2, 3} stack, val := pop(stack) fmt.Println(stack, val) }
Attempts:
2 left
💡 Hint
Check the index used to access the last element.
✗ Incorrect
The code tries to access stack[len(stack)], but valid indices go up to len(stack)-1, causing an index out of range panic.
🧠 Conceptual
advancedWhat is the time complexity of pop operation on a stack implemented with a slice?
Consider a stack implemented as a slice in Go. What is the time complexity of the pop operation?
Attempts:
2 left
💡 Hint
Popping removes the last element without shifting others.
✗ Incorrect
Popping from the end of a slice is done by slicing, which is a constant time operation.
🚀 Application
expertAfter a sequence of push and pop operations, what is the final stack?
Given the following operations on an empty stack, what is the final stack printed?
Operations:
- push 5
- push 10
- pop
- push 15
- pop
- push 20
Code snippet:
package main
import "fmt"
func main() {
stack := []int{}
stack = append(stack, 5)
stack = append(stack, 10)
stack = stack[:len(stack)-1] // pop
stack = append(stack, 15)
stack = stack[:len(stack)-1] // pop
stack = append(stack, 20)
fmt.Println(stack)
}
Attempts:
2 left
💡 Hint
Track the stack after each operation carefully.
✗ Incorrect
After pushing 5 and 10, stack is [5 10]. Pop removes 10 → [5]. Push 15 → [5 15]. Pop removes 15 → [5]. Push 20 → [5 20].
