Bird
Raised Fist0
DSA Goprogramming~20 mins

Stack Implementation Using Linked List in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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()
}
A40 -> 20 -> 10 -> null
B30 -> 20 -> 10 -> null
C20 -> 10 -> null
D40 -> 30 -> 20 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember that Push adds to the top and Pop removes from the top.
🧠 Conceptual
intermediate
1:30remaining
Understanding 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?
A3
Bnull (stack is empty)
C5
D4
Attempts:
2 left
💡 Hint
Pop removes the top element each time.
🔧 Debug
advanced
1:30remaining
Identify 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
}
Acompiles but returns zero value
Bsyntax error due to missing return statement
Creturns -1 indicating empty stack
Druntime error: invalid memory address or nil pointer dereference
Attempts:
2 left
💡 Hint
Check what happens if s.top is nil.
📝 Syntax
advanced
1:30remaining
Correct Syntax for Push Method in Stack
Which option correctly implements the Push method to add a new node at the top of the stack?
A
func (s *Stack) Push(val int) {
    s.top.value = val
    s.top.next = &Node{}
}
B
func (s *Stack) Push(val int) {
    newNode := &Node{value: val, next: s.top}
    s.top = newNode
}
C
func (s *Stack) Push(val int) {
    newNode := Node{value: val}
    s.top.next = &newNode
    s.top = &newNode
}
D
func (s *Stack) Push(val int) {
    s.top = &Node{value: val}
}
Attempts:
2 left
💡 Hint
New node's next should point to current top before updating top.
🚀 Application
expert
2:00remaining
Stack 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?
A0
B2
C1
D3
Attempts:
2 left
💡 Hint
Track each operation carefully and count remaining elements.

Practice

(1/5)
1. What is the main advantage of implementing a stack using a linked list instead of an array?
easy
A. Faster access to middle elements
B. Uses less memory for small stacks
C. Dynamic size, no fixed limit on number of elements
D. Allows random access to elements

Solution

  1. Step 1: Understand stack size limitation in arrays

    Arrays have fixed size, so stack implemented with arrays has a fixed maximum capacity.
  2. Step 2: Linked list allows dynamic memory allocation

    Linked list nodes are created as needed, so stack size can grow or shrink dynamically without fixed limit.
  3. Final Answer:

    Dynamic size, no fixed limit on number of elements -> Option C
  4. Quick Check:

    Linked list stack size is dynamic = B [OK]
Hint: Linked list stacks grow dynamically, arrays have fixed size [OK]
Common Mistakes:
  • Thinking linked list allows random access
  • Confusing memory usage for small stacks
  • Assuming linked list is always faster
2. Which of the following is the correct way to declare a node struct for a stack using linked list in Go?
easy
A. type Node struct { data int; next Node }
B. type Node struct { data int; next *Node }
C. type Node struct { data *int; next *Node }
D. type Node struct { data int; next interface{} }

Solution

  1. Step 1: Understand pointer usage in linked list nodes

    Each node must point to the next node using a pointer type to avoid copying entire struct.
  2. Step 2: Check each option for correct pointer declaration

    type Node struct { data int; next *Node } uses *Node pointer for next, which is correct. type Node struct { data int; next Node } uses Node (value), which causes copying. type Node struct { data *int; next *Node } uses *int for data which is unnecessary. type Node struct { data int; next interface{} } uses interface{} which is not type safe.
  3. Final Answer:

    type Node struct { data int; next *Node } -> Option B
  4. Quick Check:

    Next pointer must be *Node = A [OK]
Hint: Next node must be a pointer to Node struct [OK]
Common Mistakes:
  • Using value type instead of pointer for next
  • Using pointer for data unnecessarily
  • Using interface{} instead of concrete type
3. What will be the output of the following Go code snippet implementing a stack push and pop?
package main
import "fmt"

type Node struct {
  data int
  next *Node
}

var top *Node = nil

func push(val int) {
  newNode := &Node{data: val, next: top}
  top = newNode
}

func pop() int {
  if top == nil {
    return -1
  }
  val := top.data
  top = top.next
  return val
}

func main() {
  push(10)
  push(20)
  push(30)
  fmt.Println(pop())
  fmt.Println(pop())
}
medium
A. 30 20
B. 10 20
C. 20 30
D. 10 30

Solution

  1. Step 1: Trace push operations

    push(10) makes top point to node with 10; push(20) makes top point to node with 20 -> 10; push(30) makes top point to node with 30 -> 20 -> 10.
  2. Step 2: Trace pop operations

    First pop returns 30 and moves top to 20 -> 10; second pop returns 20 and moves top to 10.
  3. Final Answer:

    30 20 -> Option A
  4. Quick Check:

    Stack LIFO order = 30 then 20 [OK]
Hint: Stack pops last pushed value first (LIFO) [OK]
Common Mistakes:
  • Confusing push order with pop order
  • Returning wrong node data on pop
  • Not updating top pointer after pop
4. Identify the error in this pop function for a stack implemented using linked list in Go:
func pop() int {
  if top.next == nil {
    return -1
  }
  val := top.data
  top = top.next
  return val
}
medium
A. It does not check if top is nil before accessing top.next
B. It returns -1 incorrectly when stack is empty
C. It does not update top pointer after pop
D. It returns the wrong value from the stack

Solution

  1. Step 1: Analyze nil checks in pop function

    The function checks if top.next == nil but does not check if top itself is nil, which can cause runtime panic if stack is empty.
  2. Step 2: Understand correct empty stack check

    Correct check is if top == nil to detect empty stack before accessing top.next.
  3. Final Answer:

    It does not check if top is nil before accessing top.next -> Option A
  4. Quick Check:

    Check top == nil before top.next [OK]
Hint: Always check top == nil before accessing top.next [OK]
Common Mistakes:
  • Checking top.next before top
  • Returning wrong error value
  • Forgetting to update top pointer
5. Given a stack implemented using a linked list, how would you implement a function to reverse the stack using only stack operations (push and pop) and no extra data structures?
hard
A. Traverse linked list and swap data values pairwise
B. Use a temporary array to store elements, then push back
C. Create a new stack and push elements popped from original stack
D. Use recursion: pop all elements, then push them back in reverse order

Solution

  1. Step 1: Understand constraints of no extra data structures

    Using only push and pop means no arrays or new stacks allowed; recursion uses call stack implicitly.
  2. Step 2: Use recursion to pop all elements and then push back in reverse

    Recursively pop elements until stack is empty, then insert elements at bottom recursively to reverse order.
  3. Final Answer:

    Use recursion: pop all elements, then push them back in reverse order -> Option D
  4. Quick Check:

    Recursion reverses stack without extra data structures [OK]
Hint: Recursion can reverse stack using call stack only [OK]
Common Mistakes:
  • Using extra arrays or stacks violating constraints
  • Trying to swap node data directly
  • Not handling empty stack base case in recursion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard