What is a key advantage of using a linked list to implement a stack?
A linked list stack can grow or shrink dynamically without a fixed size limit, so it uses memory efficiently for varying stack sizes.
Click to reveal answer
beginner
What is a main limitation of an array-based stack?
An array stack has a fixed size, so it can overflow if too many elements are pushed beyond its capacity.
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 in average cases.
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.
Click to reveal answer
intermediate
What is a trade-off when choosing linked list stack over array stack?
Linked list stacks use extra memory for storing pointers and may have slower access due to non-contiguous memory, but they avoid fixed size limits.
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 can grow dynamically because nodes are allocated as needed, unlike fixed-size arrays.
What is a disadvantage of using an array stack?
ASlower push and pop operations
BFixed size can cause overflow
CUses extra memory for pointers
DNon-contiguous memory storage
✗ Incorrect
Array stacks have a fixed size, so pushing beyond capacity causes overflow.
Which stack implementation typically has better cache performance?
AArray stack
BDepends on the programming language
CBoth have equal cache performance
DLinked list stack
✗ Incorrect
Array stacks store elements contiguously, improving cache locality and speed.
What extra memory cost does a linked list stack have compared to an array stack?
AMemory for array resizing
BMemory for stack overflow handling
CNo extra memory cost
DMemory for storing pointers in each node
✗ Incorrect
Each node in a linked list stores a pointer to the next node, adding memory overhead.
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 is O(1) in both implementations as it adds an element at the top.
Explain the main trade-offs between using a linked list stack and an array stack.
Think about memory use, speed, and size limits.
You got /4 concepts.
Describe scenarios where you would prefer a linked list stack over an array stack and why.
Consider flexibility and memory trade-offs.
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. Accessing elements by index is faster.
B. Memory usage is always less than an array.
C. The stack size can grow dynamically without a fixed limit.
D. It uses less memory overhead per element.
Solution
Step 1: Understand stack size behavior in linked list
A linked list stack can grow dynamically as nodes are added, limited only by available memory.
Step 2: Compare with array stack size
An array stack has a fixed size or requires resizing, which can be costly and limits immediate growth.
Final Answer:
The stack size can grow dynamically without a fixed limit. -> Option C
Quick Check:
Dynamic size = B [OK]
Hint: Linked list stacks grow dynamically; arrays have fixed size [OK]
Common Mistakes:
Thinking array stacks grow dynamically without resizing
Assuming linked list uses less memory always
Confusing access speed advantages
2. Which of the following is the correct way to declare an array-based stack of integers in Java?
easy
A. int stack = new int[10];
B. int[] stack = new int[10];
C. int[] stack = int[10];
D. int stack[] = int(10);
Solution
Step 1: Recall Java array declaration syntax
In Java, arrays are declared with type[], variable = new type[size];
Step 2: Check each option
int[] stack = new int[10]; matches correct syntax: int[] stack = new int[10]; others have syntax errors.
Final Answer:
int[] stack = new int[10]; -> Option B
Quick Check:
Correct Java array syntax = A [OK]
Hint: Java arrays use type[] var = new type[size]; [OK]
Common Mistakes:
Missing 'new' keyword
Using parentheses instead of brackets
Declaring array without []
3. Consider the following Java code implementing a stack using a linked list. What will be printed after pushing 10, 20, then popping once?
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
class Stack {
Node top;
void push(int x) {
Node n = new Node(x);
n.next = top;
top = n;
}
int pop() {
if (top == null) return -1;
int val = top.data;
top = top.next;
return val;
}
}
public class Main {
public static void main(String[] args) {
Stack s = new Stack();
s.push(10);
s.push(20);
System.out.println(s.pop());
}
}
medium
A. 20
B. null
C. 10
D. Compilation error
Solution
Step 1: Trace push operations
Push 10: top points to node with 10. Push 20: top points to node with 20, next points to 10.
Step 2: Trace pop operation
Pop removes top node (20), returns 20, top now points to node with 10.
Final Answer:
20 -> Option A
Quick Check:
Last pushed, first popped = 20 [OK]
Hint: Stack pop returns last pushed value [OK]
Common Mistakes:
Confusing order of push and pop
Assuming pop returns bottom element
Ignoring null check in pop
4. The following code attempts to implement a stack using an array but has a bug. What is the error?
class ArrayStack {
int[] stack = new int[5];
int top = -1;
void push(int x) {
top++;
stack[top] = x;
}
}
medium
A. stack array should be declared static
B. top should start at 0, not -1
C. Array size should be dynamic
D. No check for stack overflow before push
Solution
Step 1: Analyze push method
Push increments top and assigns value without checking if top exceeds array size.
Step 2: Identify missing overflow check
Without checking top < stack.length, pushing beyond capacity causes ArrayIndexOutOfBoundsException.
Final Answer:
No check for stack overflow before push -> Option D
Quick Check:
Missing overflow check = A [OK]
Hint: Always check stack size before pushing [OK]
Common Mistakes:
Assuming top starts at 0 is mandatory
Thinking array must be dynamic always
Confusing static keyword necessity
5. You need to implement a stack for a program where the maximum number of elements is unknown and memory efficiency is important. Which implementation is best and why?
hard
A. Linked list stack, because it grows dynamically and uses memory only as needed.
B. Linked list stack, because it uses less memory overhead per element than arrays.
C. Array stack, because it provides faster push and pop operations always.
D. Array stack, because arrays use less memory per element and resizing is automatic.
Solution
Step 1: Consider unknown max size and memory efficiency
Unknown max size means dynamic growth is needed; linked list grows as needed without fixed size.
Step 2: Compare memory usage and operation speed
Linked list uses extra memory per node for pointers but avoids wasted space from large arrays; resizing arrays can be costly.
Step 3: Choose best fit
Linked list stack fits unknown size and memory efficiency better despite pointer overhead.
Final Answer:
Linked list stack, because it grows dynamically and uses memory only as needed. -> Option A
Quick Check:
Dynamic size + memory efficient = D [OK]
Hint: Unknown size? Use linked list stack for dynamic memory [OK]
Common Mistakes:
Assuming arrays resize automatically without cost
Ignoring pointer memory overhead in linked lists
Confusing speed with memory efficiency
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.