Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Push and Pop Operations on Stack
What is the printed state of the stack after the following operations?
DSA Go
package main import "fmt" type Node struct { value int next *Node } type Stack struct { top *Node } func (s *Stack) Push(val int) { newNode := &Node{value: val, next: s.top} s.top = newNode } func (s *Stack) Pop() int { if s.top == nil { return -1 } val := s.top.value s.top = s.top.next return val } func (s *Stack) Print() { current := s.top for current != nil { fmt.Printf("%d -> ", current.value) current = current.next } fmt.Println("null") } func main() { s := &Stack{} s.Push(10) s.Push(20) s.Push(30) s.Pop() s.Push(40) s.Print() }
Attempts:
2 left
💡 Hint
Remember that Push adds to the top and Pop removes from the top.
✗ Incorrect
Push adds nodes to the top of the stack. After pushing 10, 20, 30, the stack is 30 -> 20 -> 10 -> null. Pop removes the top (30), so stack becomes 20 -> 10 -> null. Then pushing 40 adds it on top: 40 -> 20 -> 10 -> null.
🧠 Conceptual
intermediateUnderstanding Stack Top Pointer After Multiple Pops
If a stack implemented using a linked list has elements 5 -> 4 -> 3 -> null (top is 5), what will be the top element after two Pop operations?
Attempts:
2 left
💡 Hint
Pop removes the top element each time.
✗ Incorrect
First Pop removes 5, stack becomes 4 -> 3 -> null. Second Pop removes 4, so top becomes 3.
🔧 Debug
advancedIdentify the Bug in Stack Pop Method
What error will occur when running this Pop method if the stack is empty?
DSA Go
func (s *Stack) Pop() int { val := s.top.value s.top = s.top.next return val }
Attempts:
2 left
💡 Hint
Check what happens if s.top is nil.
✗ Incorrect
If s.top is nil (empty stack), accessing s.top.value causes a runtime panic due to nil pointer dereference.
📝 Syntax
advancedCorrect Syntax for Push Method in Stack
Which option correctly implements the Push method to add a new node at the top of the stack?
Attempts:
2 left
💡 Hint
New node's next should point to current top before updating top.
✗ Incorrect
Option B correctly creates a new node pointing to current top and updates top to new node. Others either overwrite existing nodes or lose the chain.
🚀 Application
expertStack Size After Mixed Operations
Given a stack implemented with a linked list, initially empty, the following operations are performed in order: Push(1), Push(2), Pop(), Push(3), Pop(), Pop(), Push(4). How many elements are in the stack at the end?
Attempts:
2 left
💡 Hint
Track each operation carefully and count remaining elements.
✗ Incorrect
Operations:
- Push(1): stack = 1
- Push(2): stack = 2 -> 1
- Pop(): removes 2, stack = 1
- Push(3): stack = 3 -> 1
- Pop(): removes 3, stack = 1
- Pop(): removes 1, stack empty
- Push(4): stack = 4
So only one element remains.
