A dynamic stack using a resizable array is a stack data structure that grows or shrinks its underlying array automatically when elements are pushed or popped, so it can handle any number of elements without fixed size limits.
Click to reveal answer
beginner
Why do we resize the array in a dynamic stack?
We resize the array to save memory and allow the stack to grow beyond its initial capacity. When the array is full, we create a bigger array and copy elements. When the stack is mostly empty, we shrink the array to avoid wasting space.
Click to reveal answer
intermediate
What happens when you push an element and the array is full?
When pushing and the array is full, the stack doubles the size of the array, copies all elements to the new array, then adds the new element. This ensures there is always space for new elements.
Click to reveal answer
intermediate
How does popping elements affect the size of the array?
When popping elements, if the number of elements becomes less than a quarter of the array size, the stack shrinks the array to half its current size to save memory.
Click to reveal answer
intermediate
What is the time complexity of push and pop operations in a dynamic stack using a resizable array?
Push and pop operations are usually O(1) (constant time). Occasionally, resizing takes O(n) time, but amortized over many operations, the average time remains O(1).
Click to reveal answer
What triggers the resizing of the array in a dynamic stack?
AOnly when the array is full
BWhen the array is full or less than a quarter full
COnly when the array is empty
DWhen the array is half full
✗ Incorrect
The array resizes by growing when full and shrinking when less than a quarter full.
What is the initial size of the array in a dynamic stack?
AIt can be any fixed size, often small like 1 or 2
BAlways 100
CAlways 0
DIt must be very large to avoid resizing
✗ Incorrect
The initial size is usually small and grows dynamically as needed.
What is the main advantage of using a dynamic stack over a fixed-size stack?
AIt is faster for all operations
BIt uses less memory always
CIt can handle any number of elements without overflow
DIt never needs resizing
✗ Incorrect
Dynamic stacks grow as needed, so they avoid overflow errors.
Which operation might take longer than O(1) time in a dynamic stack?
AResizing the array during push or pop
BSimple push without resizing
CSimple pop without resizing
DChecking if the stack is empty
✗ Incorrect
Resizing requires copying elements, which takes O(n) time.
What happens if you pop from an empty dynamic stack?
AIt returns a default value
BThe stack resizes automatically
CNothing happens
DAn error or exception occurs
✗ Incorrect
Popping from an empty stack is invalid and usually causes an error.
Explain how a dynamic stack using a resizable array manages memory when pushing and popping elements.
Think about when and why the array size changes.
You got /4 concepts.
Describe the time complexity of push and pop operations in a dynamic stack and why resizing does not make them slow on average.
Consider how often resizing occurs compared to normal operations.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using a dynamic stack implemented with a resizable array over a fixed-size array stack?
easy
A. It always has a fixed capacity.
B. It uses less memory than a linked list stack.
C. It does not require resizing when full.
D. It can grow or shrink automatically as elements are pushed or popped.
B. It does not check for empty stack before popping.
C. It shrinks the array too early, causing frequent resizing.
D. It doubles the array size instead of shrinking.
Solution
Step 1: Understand shrinking condition
The array shrinks when top is less than one-fourth of array length, which can cause frequent resizing if elements fluctuate near this threshold.
Step 2: Identify problem with frequent resizing
Frequent shrinking and expanding wastes time and resources, so better to shrink less aggressively.
Final Answer:
It shrinks the array too early, causing frequent resizing. -> Option C
Quick Check:
Shrink threshold too high = frequent resize [OK]
Hint: Shrink array only when usage is very low to avoid thrashing [OK]
Common Mistakes:
Ignoring empty stack check (it is present)
Confusing shrinking with doubling
Not clearing popped element
5. You want to implement a dynamic stack that minimizes memory usage by shrinking the internal array only when the number of elements is less than 1/8 of the array size. Which change should you make to the pop method's shrinking condition?