A stack is a collection where elements are added and removed from only one end called the top. It follows Last In, First Out (LIFO) order, like a stack of plates where you add or remove the top plate first.
Click to reveal answer
beginner
What is the role of a linked list in implementing a stack?
A linked list helps implement a stack by using nodes connected one after another. The top of the stack points to the first node. Adding or removing elements means changing the top pointer to the next node.
Click to reveal answer
intermediate
Explain the push operation in a stack implemented with a linked list.
Push adds a new node at the top. We create a new node, set its next pointer to the current top, then update the top pointer to this new node.
Click to reveal answer
intermediate
Explain the pop operation in a stack implemented with a linked list.
Pop removes the top node. We save the current top node, update the top pointer to the next node, and return the removed node's value.
Click to reveal answer
beginner
What happens if you pop from an empty stack implemented with a linked list?
If the stack is empty (top is nil), popping returns an error or nil value because there is no element to remove.
Click to reveal answer
In a stack implemented with a linked list, where is the new element added during push?
AAt the bottom of the stack
BAt a random position
CIn the middle of the stack
DAt the top of the stack
✗ Incorrect
Push always adds the new element at the top of the stack.
What does the top pointer represent in a linked list stack?
AThe last node in the list
BThe first node in the list
CThe middle node in the list
DA random node
✗ Incorrect
The top pointer points to the first node, which is the top of the stack.
What is the time complexity of push and pop operations in a linked list stack?
AO(1)
BO(log n)
CO(n)
DO(n^2)
✗ Incorrect
Both push and pop operate in constant time O(1) because they only change the top pointer.
What happens to the top pointer after a pop operation?
AIt moves to the next node
BIt stays the same
CIt moves to the previous node
DIt becomes nil always
✗ Incorrect
After pop, the top pointer moves to the next node in the linked list.
Which data structure property does a stack follow?
ARandom Access
BFirst In, First Out (FIFO)
CLast In, First Out (LIFO)
DSorted Order
✗ Incorrect
Stack follows Last In, First Out (LIFO) property.
Describe how push and pop operations work in a stack implemented using a linked list.
Think about how the top pointer changes during push and pop.
You got /4 concepts.
Explain why a linked list is a good choice to implement a stack.
Consider how linked lists handle memory and node connections.
You got /4 concepts.
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
Step 1: Understand stack size limitation in arrays
Arrays have fixed size, so stack implemented with arrays has a fixed maximum capacity.
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.
Final Answer:
Dynamic size, no fixed limit on number of elements -> Option C
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
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.
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.
Final Answer:
type Node struct { data int; next *Node } -> Option B
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
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.
Step 2: Trace pop operations
First pop returns 30 and moves top to 20 -> 10; second pop returns 20 and moves top to 10.
Final Answer:
30
20 -> Option A
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
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.
Step 2: Understand correct empty stack check
Correct check is if top == nil to detect empty stack before accessing top.next.
Final Answer:
It does not check if top is nil before accessing top.next -> Option A
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
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.
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.
Final Answer:
Use recursion: pop all elements, then push them back in reverse order -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.