What is a key advantage of using a linked list to implement a stack?
A linked list stack can grow dynamically without a fixed size limit, so it uses memory efficiently and avoids overflow unless system memory is exhausted.
Click to reveal answer
beginner
What is a main disadvantage of using an array to implement a stack?
An array stack has a fixed size, so it can overflow if too many elements are pushed, or waste memory if the size is too large.
Click to reveal answer
intermediate
How does the time complexity of push and pop operations compare between linked list and array stacks?
Both linked list and array stacks have O(1) time complexity for push and pop operations, meaning they are very fast and constant time.
Click to reveal answer
intermediate
Why might an array stack have better cache performance than a linked list stack?
Array stacks store elements in contiguous memory locations, which improves CPU cache usage and speeds up access compared to linked lists with scattered nodes.
Click to reveal answer
advanced
What is a trade-off when choosing between linked list and array stack implementations?
Linked list stacks offer flexible size but use extra memory for pointers; array stacks use less memory per element but have fixed size and possible resizing overhead.
Click to reveal answer
Which stack implementation can dynamically grow without a fixed size limit?
AArray stack
BLinked list stack
CBoth linked list and array stacks
DNeither linked list nor array stacks
✗ Incorrect
Linked list stacks grow dynamically by adding nodes, while array stacks have a fixed size unless resized.
What is the time complexity of push operation in both linked list and array stacks?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ Incorrect
Push operation in both implementations is constant time O(1).
Which stack implementation typically uses more memory per element due to extra pointers?
ALinked list stack
BDepends on the programming language
CBoth use the same memory
DArray stack
✗ Incorrect
Linked list nodes store data plus pointers, increasing memory usage per element.
Why might an array stack be faster in practice despite similar time complexity?
ABecause arrays resize automatically
BBecause arrays use pointers
CBecause arrays have slower access times
DBecause arrays use contiguous memory improving cache performance
✗ Incorrect
Contiguous memory in arrays improves CPU cache hits, speeding up access.
What happens if you push more elements than the fixed size of an array stack?
AStack overflows or needs resizing
BStack automatically converts to linked list
CPush operation silently fails
DStack shrinks automatically
✗ Incorrect
Array stacks have fixed size; pushing beyond capacity causes overflow or requires resizing.
Explain the main memory and size trade-offs between linked list and array stack implementations.
Think about how memory is allocated and used in both.
You got /4 concepts.
Describe why array stacks might have better performance in practice despite both having O(1) push/pop operations.
Consider how computer memory and CPU cache work.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is a key advantage of using a linked list to implement a stack compared to using an array?
easy
A. Dynamic size without needing to resize
B. Faster access to elements by index
C. Uses less memory per element
D. Simpler syntax in Go
Solution
Step 1: Understand stack size behavior
Array stacks have fixed size or need resizing, linked lists grow dynamically.
Step 2: Compare advantages
Linked list stacks do not require resizing, so they handle dynamic size better.
Final Answer:
Dynamic size without needing to resize -> Option A
Quick Check:
Linked list stacks grow dynamically = B [OK]
Hint: Linked lists grow dynamically, arrays need resizing [OK]
Common Mistakes:
Thinking arrays always use less memory
Confusing access speed advantage
Assuming syntax is simpler for linked lists
2. Which Go syntax correctly declares an array-based stack with capacity 5?
easy
A. stack := make([]int, 5)
B. stack := [5]int{}
C. var stack []int
D. var stack []int = [5]int{}
Solution
Step 1: Identify array declaration syntax
In Go, arrays are declared as [size]type{}, e.g., [5]int{}.
Step 2: Check options
stack := [5]int{} uses correct syntax for array with size 5 and int type.
Final Answer:
stack := [5]int{} -> Option B
Quick Check:
Array declaration = [5]int{} [OK]
Hint: Arrays use [size]type{}, slices use make() [OK]
Common Mistakes:
Confusing slices with arrays
Using make() for arrays
Incorrect variable declaration syntax
3. Consider this Go code implementing a stack using an array:
type Stack struct {
items [3]int
top int
}
func (s *Stack) Push(val int) bool {
if s.top == len(s.items) {
return false
}
s.items[s.top] = val
s.top++
return true
}
func (s *Stack) Pop() (int, bool) {
if s.top == 0 {
return 0, false
}
s.top--
return s.items[s.top], true
}
func main() {
s := Stack{}
s.Push(10)
s.Push(20)
s.Push(30)
s.Push(40) // extra push
val, ok := s.Pop()
fmt.Println(val, ok)
}
What is the output?
medium
A. 20 true
B. 40 true
C. 30 true
D. 0 false
Solution
Step 1: Trace Push operations
Push 10, 20, 30 succeed; Push 40 fails because stack is full (size 3).
Step 2: Trace Pop operation
Pop returns last pushed value 30 and true.
Final Answer:
30 true -> Option C
Quick Check:
Last pushed valid value = 30 [OK]
Hint: Stack size limits extra pushes; pop returns last valid push [OK]
Common Mistakes:
Assuming push always succeeds
Thinking pop returns failed push value
Confusing top index after push/pop
4. This Go code attempts to implement a linked list stack but has a bug:
type Node struct {
value int
next *Node
}
type Stack struct {
head *Node
}
func (s *Stack) Push(val int) {
newNode := &Node{value: val}
newNode.next = s.head
s.head = newNode
}
func (s *Stack) Pop() (int, bool) {
if s.head == nil {
return 0, false
}
val := s.head.value
s.head = s.head.next
return val, true
}
func main() {
s := Stack{}
s.Push(1)
s.Push(2)
s.Pop()
s.Pop()
s.Pop() // extra pop
}
What is the bug in this code?
medium
A. Extra pop returns false but is ignored, no bug
B. Push does not update the head pointer
C. Pop does not check for empty stack before accessing value
D. Node struct missing initialization of next pointer
Solution
Step 1: Analyze Pop safety check
Pop checks if head is nil before accessing value, so no crash.
Step 2: Check extra pop behavior
Extra pop returns (0, false), which is safe but ignored in main.
Final Answer:
Extra pop returns false but is ignored, no bug -> Option A
Quick Check:
Pop safe on empty stack = A [OK]
Hint: Check nil before pop; ignoring false return is not a bug [OK]
Common Mistakes:
Assuming pop crashes on empty stack
Thinking push forgets to update head
Believing next pointer needs explicit nil init
5. You need to implement a stack in Go that can handle an unknown number of elements efficiently and minimize memory waste. Which implementation is best and why?
Options:
A) Array stack with large fixed size
B) Linked list stack
C) Array stack with dynamic resizing
D) Slice-based stack without capacity checks
hard
A. Array stack with large fixed size
B. Linked list stack
C. Slice-based stack without capacity checks
D. Array stack with dynamic resizing
Solution
Step 1: Consider unknown size and memory use
Large fixed arrays waste memory; linked lists have overhead per node.
Step 2: Evaluate dynamic resizing arrays
Dynamic resizing arrays grow as needed, balancing memory and speed efficiently.
Step 3: Assess slice without capacity checks
Slice without checks risks runtime errors or inefficient growth.
Final Answer:
Array stack with dynamic resizing -> Option D
Quick Check:
Dynamic resizing balances size and memory = C [OK]
Hint: Dynamic arrays grow as needed, saving memory and time [OK]
Common Mistakes:
Choosing fixed size wastes memory
Ignoring overhead of linked lists
Using slices without capacity checks causes errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.