Challenge - 5 Problems
Stack Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after pushing elements on a stack?
Consider a stack implemented using a slice in Go. What will be the printed stack after pushing 10, then 20, then 30?
DSA Go
package main import ( "fmt" ) type Stack struct { items []int } func (s *Stack) Push(val int) { s.items = append(s.items, val) } func (s *Stack) Print() { for i := len(s.items) - 1; i >= 0; i-- { fmt.Print(s.items[i]) if i != 0 { fmt.Print(" -> ") } } fmt.Println(" -> null") } func main() { stack := Stack{} stack.Push(10) stack.Push(20) stack.Push(30) stack.Print() }
Attempts:
2 left
💡 Hint
Remember that stack is Last In First Out (LIFO). The last pushed element is on top.
✗ Incorrect
The Push method appends elements to the slice. Printing from the last index to the first shows the top of the stack first. So after pushing 10, 20, 30, the top is 30, then 20, then 10.
❓ Predict Output
intermediateWhat happens if you push on a nil stack slice?
In Go, what will be the output of the following code when pushing 5 onto a nil slice stack and then printing?
DSA Go
package main import "fmt" type Stack struct { items []int } func (s *Stack) Push(val int) { s.items = append(s.items, val) } func (s *Stack) Print() { for i := len(s.items) - 1; i >= 0; i-- { fmt.Print(s.items[i]) if i != 0 { fmt.Print(" -> ") } } fmt.Println(" -> null") } func main() { var stack Stack stack.Push(5) stack.Print() }
Attempts:
2 left
💡 Hint
Appending to a nil slice in Go creates a new slice with the element.
✗ Incorrect
In Go, appending to a nil slice works fine and creates a new slice with the appended element. So pushing 5 on a nil slice results in a slice with one element 5.
🔧 Debug
advancedWhy does this push method cause a runtime error?
Identify the error in this push method for a stack implemented with a slice in Go.
DSA Go
func (s *Stack) Push(val int) { var newItems []int newItems[0] = val s.items = append(newItems, s.items...) }
Attempts:
2 left
💡 Hint
Check how slices are initialized and indexed before assignment.
✗ Incorrect
newItems is declared but empty (nil slice). Assigning to newItems[0] causes an index out of range panic because the slice has length 0. You must append or initialize with length before assigning by index.
🧠 Conceptual
advancedWhat is the time complexity of the push operation on a stack implemented with a slice in Go?
Consider a stack implemented using a Go slice. What is the average time complexity of the Push operation?
Attempts:
2 left
💡 Hint
Appending to a slice sometimes requires resizing the underlying array.
✗ Incorrect
Appending to a slice is usually O(1) but occasionally requires resizing the underlying array, which takes O(n). Over many pushes, the average time per push is O(1) amortized.
🚀 Application
expertAfter pushing elements, what is the state of the stack?
Given the following Go code that pushes elements onto a stack, what is the printed output?
DSA Go
package main import "fmt" type Stack struct { items []int } func (s *Stack) Push(val int) { s.items = append(s.items, val) } func (s *Stack) Print() { for i := len(s.items) - 1; i >= 0; i-- { fmt.Print(s.items[i]) if i != 0 { fmt.Print(" -> ") } } fmt.Println(" -> null") } func main() { stack := Stack{} stack.Push(1) stack.Push(2) stack.Push(3) stack.Push(4) stack.Push(5) stack.Print() }
Attempts:
2 left
💡 Hint
Remember the stack is LIFO, so the last pushed element is on top.
✗ Incorrect
Elements are pushed in order 1,2,3,4,5. The top of the stack is the last pushed element 5, so printing from top to bottom shows 5 -> 4 -> 3 -> 2 -> 1 -> null.
