A stack that can grow or shrink in size by resizing its underlying array when needed, allowing flexible storage without fixed limits.
Click to reveal answer
beginner
Why do we resize the array in a dynamic stack?
To add more space when the stack is full or reduce space when many elements are removed, optimizing memory use.
Click to reveal answer
intermediate
What happens during the push operation if the stack is full?
The array size is doubled, existing elements are copied to the new larger array, then the new element is added.
Click to reveal answer
intermediate
How does the pop operation affect the array size in a dynamic stack?
If the number of elements becomes much smaller than the array size (usually less than a quarter), the array size is halved to save space.
Click to reveal answer
intermediate
What is the time complexity of push and pop in a dynamic stack using a resizable array?
Amortized time complexity is O(1) for both push and pop, because resizing happens rarely and copying cost is spread out.
Click to reveal answer
What triggers the resizing of the array in a dynamic stack?
AWhen the stack has exactly one element
BWhen the stack is full or too empty
COnly when the stack is full
DOnly when the stack is empty
✗ Incorrect
The array resizes when full (to grow) or when too empty (to shrink).
What is the usual factor by which the array size is increased during resizing?
ADoubled
BTripled
CIncreased by one
DHalved
✗ Incorrect
The array size is usually doubled to balance between resizing frequency and memory use.
What is the main advantage of using a dynamic stack over a fixed-size stack?
AIt is slower
BIt uses less memory always
CIt never needs resizing
DIt can handle more elements without running out of space
✗ Incorrect
Dynamic stacks can grow as needed, unlike fixed-size stacks with limited capacity.
What happens to elements during resizing of the stack's array?
AThey are deleted
BThey stay in the old array
CThey are copied to the new array
DThey are reversed
✗ Incorrect
Elements are copied to the new larger or smaller array to preserve the stack content.
What is the amortized time complexity of push operation in a dynamic stack?
AO(1)
BO(n)
CO(log n)
DO(n^2)
✗ Incorrect
Push is O(1) amortized because resizing happens rarely and cost is spread out.
Explain how a dynamic stack resizes its array during push and pop operations.
Think about when the stack grows and shrinks.
You got /3 concepts.
Describe the benefits of using a dynamic stack with a resizable array compared to a fixed-size stack.
Consider memory and capacity.
You got /3 concepts.
Practice
(1/5)
1. What is the main advantage of using a dynamic stack implemented with a resizable array compared to a fixed-size array stack?
easy
A. It uses less memory than a linked list stack.
B. It always has a fixed maximum size.
C. It can grow or shrink in size as needed during runtime.
D. It does not require any resizing operations.
Solution
Step 1: Understand fixed vs dynamic size
A fixed-size array stack has a set capacity that cannot change during runtime.
Step 2: Identify dynamic array behavior
A dynamic stack with a resizable array can increase or decrease its size as elements are pushed or popped.
Final Answer:
It can grow or shrink in size as needed during runtime. -> Option C
Quick Check:
Dynamic resizing means flexible size [OK]
Hint: Dynamic arrays resize to fit data, unlike fixed arrays [OK]
Common Mistakes:
Thinking dynamic stack has fixed size
Confusing memory usage with resizing
Assuming no resizing is needed
2. Which of the following is the correct syntax to double the size of a dynamic stack's array in C when it is full?
easy
A. stack->array = realloc(stack->array, stack->capacity * 2 * sizeof(int));
B. stack->array = malloc(stack->capacity * sizeof(int));
C. stack->array = realloc(stack->array, stack->capacity / 2 * sizeof(int));
D. stack->array = realloc(stack->array, sizeof(int));
Solution
Step 1: Identify correct use of realloc
realloc is used to resize the memory block; new size should be double current capacity times element size.
Step 2: Check each option
stack->array = realloc(stack->array, stack->capacity * 2 * sizeof(int)); correctly doubles capacity and multiplies by sizeof(int). Others either allocate wrong size or use malloc instead of realloc.
Hint: Never resize array to zero or negative size [OK]
Common Mistakes:
Ignoring minimum capacity limit
Confusing overflow with underflow
Misusing post-decrement operator
5. You want to implement a dynamic stack using a resizable array that shrinks its capacity when the number of elements falls below one-fourth of the capacity, but never shrinks below 2. Which modification to the pop function below ensures this behavior?
int pop(Stack *stack) {
if (stack->top == -1) {
printf("Stack Underflow\n");
return -1;
}
int value = stack->array[stack->top--];
if (stack->top < stack->capacity / 4) {
stack->capacity /= 2;
stack->array = realloc(stack->array, stack->capacity * sizeof(int));
}
return value;
}
hard
A. Remove the resizing code entirely.
B. Add a check: if (stack->capacity > 2) before halving capacity.
C. Change condition to if (stack->top < stack->capacity / 2).
D. Set capacity to 1 after halving.
Solution
Step 1: Understand shrinking condition
Stack shrinks when elements < 1/4 capacity but must not go below 2 capacity.
Step 2: Add capacity lower bound check
Before halving capacity, check if capacity is greater than 2 to avoid shrinking below 2.
Final Answer:
Add a check: if (stack->capacity > 2) before halving capacity. -> Option B
Quick Check:
Prevent capacity below 2 by conditional check [OK]
Hint: Check capacity > 2 before shrinking to avoid too small array [OK]
Common Mistakes:
Shrinking capacity below minimum allowed
Changing condition incorrectly
Removing resizing causes memory waste
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.