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 an array-based stack compared to a linked list stack?
An array stack has a fixed size, so it can overflow if too many elements are pushed, wasting memory if the array is too large or causing errors if too small.
Click to reveal answer
beginner
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 be faster in practice than a linked list stack?
Array stacks have better cache locality because elements are stored contiguously in memory, making access faster than linked lists which have scattered nodes.
Click to reveal answer
intermediate
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?
ALinked list stack
BArray stack
CBoth linked list and array stacks
DNeither linked list nor array stacks
✗ Incorrect
Linked list stacks allocate nodes dynamically, so they can grow as needed without a fixed size.
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).
Why might an array stack be faster than a linked list stack in practice?
ABecause arrays have better cache locality
BBecause linked lists have faster push
CBecause linked lists use fixed size
DBecause arrays use less memory
✗ Incorrect
Arrays store elements contiguously, improving cache performance and speed.
What is a disadvantage of linked list stacks compared to array stacks?
AFixed size
BExtra memory for pointers
CSlower push operation
DCannot grow dynamically
✗ Incorrect
Linked list nodes store extra pointers, increasing memory usage.
Which stack implementation risks overflow if too many elements are pushed?
ANeither linked list nor array stacks
BLinked list stack
CBoth linked list and array stacks
DArray stack
✗ Incorrect
Array stacks have fixed size and can overflow if capacity is exceeded.
Explain the main memory trade-offs between using a linked list stack and an array stack.
Think about how memory is allocated and used in both.
You got /4 concepts.
Describe why push and pop operations are O(1) in both linked list and array stack implementations.
Focus on how stacks add or remove elements.
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. Faster access to elements by index
B. Simpler to implement in all languages
C. Uses less memory overall
D. Dynamic size without needing to resize
Solution
Step 1: Understand stack size behavior
Arrays have fixed size or require resizing, linked lists grow dynamically as needed.
Step 2: Compare access and memory
Linked lists do not provide faster index access and may use more memory due to pointers.
Final Answer:
Dynamic size without needing to resize -> Option D
Quick Check:
Linked list stacks grow dynamically = B [OK]
Hint: Linked list stacks grow dynamically, arrays need resizing [OK]
Common Mistakes:
Thinking linked lists have faster index access
Assuming arrays always use less memory
Believing linked lists are simpler to implement everywhere
2. Which Rust syntax correctly declares a stack using a vector (array stack)?
easy
A. let stack: Vec<i32> = [];
B. let stack = new Vec<i32>();
C. let mut stack: Vec<i32> = Vec::new();
D. let mut stack = Vec<i32>::empty();
Solution
Step 1: Recall Rust vector initialization
Correct syntax uses Vec::new() to create an empty vector.
Step 2: Check each option
B uses invalid 'new' keyword, C uses array syntax, D uses non-existent empty() method.
Final Answer:
let mut stack: Vec<i32> = Vec::new(); -> Option C
Quick Check:
Vec::new() creates empty vector = A [OK]
Hint: Use Vec::new() with mut for stack vector in Rust [OK]
Common Mistakes:
Using 'new' keyword instead of Vec::new()
Trying to assign array literal directly
Using non-existent Vec methods
3. What will be the output of the following Rust code using a linked list stack?
struct Node {
value: i32,
next: Option,
}
fn main() {
let mut stack: Option = None;
// Push 10
stack = Some(Box::new(Node { value: 10, next: stack }));
// Push 20
stack = Some(Box::new(Node { value: 20, next: stack }));
// Pop top
if let Some(top) = stack {
println!("{}", top.value);
stack = top.next;
}
// Print new top
if let Some(top) = &stack {
println!("{}", top.value);
}
}
medium
A. 20
10
B. 10
20
C. 10
10
D. 20
20
Solution
Step 1: Trace push operations
Push 10: stack points to node with value 10. Push 20: stack points to node with value 20, next points to 10.
Step 2: Trace pop and prints
Pop prints 20, then stack moves to next node (10). Next print prints 10.
Final Answer:
20
10 -> Option A
Quick Check:
Stack top after push: 20, after pop: 10 [OK]
Hint: Top is last pushed, pop removes top, next is previous [OK]
Common Mistakes:
Confusing push order and print order
Printing after pop without updating stack
Misunderstanding Option and Box usage
4. Identify the bug in this Rust code implementing a stack with an array (Vec) and fix it:
fn main() {
let mut stack: Vec<i32> = Vec::new();
stack.push(1);
stack.push(2);
let top = stack.pop();
println!("Top element: {}", top.unwrap());
println!("Next element: {}", stack.pop().unwrap());
println!("Empty pop: {}", stack.pop().unwrap());
}
medium
A. Calling unwrap() on empty pop causes panic; check Option before unwrap
B. Vec::new() should be Vec::with_capacity()
C. Use stack.peek() instead of pop() to view top
D. stack.push() should be called after pop()
Solution
Step 1: Analyze pop calls and unwrap
First two pops succeed, third pop returns None; unwrap on None panics.
Step 2: Fix by checking Option before unwrap
Use if let or match to safely handle empty stack before unwrap.
Final Answer:
Calling unwrap() on empty pop causes panic; check Option before unwrap -> Option A
Quick Check:
Pop returns Option, unwrap on None panics [OK]
Hint: Always check Option before unwrap after pop [OK]
Common Mistakes:
Ignoring possibility of empty stack pop
Thinking Vec::with_capacity fixes panic
Using peek instead of pop changes logic
5. You need to implement a stack in Rust for a program where the maximum number of elements is unknown and can grow large unpredictably. Which implementation is better and why?
Array stack using Vec with occasional resizing
Linked list stack with dynamic nodes
hard
A. Array stack is better because it uses less memory per element and faster cache access
B. Linked list stack is better because it grows dynamically without resizing overhead
C. Array stack is better because resizing never happens in Rust Vec
D. Linked list stack is better because it uses contiguous memory for faster access
Solution
Step 1: Consider unknown large size and growth
Linked list stacks grow node by node without resizing, arrays resize which can be costly.
Step 2: Compare memory and speed trade-offs
Arrays have better cache but resizing overhead; linked lists avoid resizing but use extra memory per node.
Step 3: Choose best fit for unpredictable large growth
Linked list avoids resizing delays and can handle unpredictable growth smoothly.
Final Answer:
Linked list stack is better because it grows dynamically without resizing overhead -> Option B
Quick Check:
Dynamic growth without resizing = Linked list stack [OK]
Hint: For unknown large size, linked list avoids resizing delays [OK]
Common Mistakes:
Assuming Vec never resizes
Ignoring memory overhead of linked list nodes
Confusing contiguous memory with linked list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.