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, allowing flexible size without fixed limits.
Click to reveal answer
beginner
Why do we resize the array in a dynamic stack?
We resize the array to handle more elements when the stack is full or to save memory when many elements are removed, ensuring efficient use of space and avoiding overflow errors.
Click to reveal answer
beginner
In Rust, which method is commonly used to add an element to the end of a vector?
The method is push(), which appends an element to the end of the vector, increasing its length by one.
Click to reveal answer
beginner
What happens when you pop an element from an empty dynamic stack?
Popping from an empty dynamic stack returns None or an error, depending on implementation, because there are no elements to remove.
Click to reveal answer
intermediate
How does doubling the array size during resizing help performance?
Doubling the array size reduces the number of times resizing happens, making push operations mostly fast (amortized constant time), balancing speed and memory use.
Click to reveal answer
What is the main advantage of using a dynamic stack over a fixed-size stack?
AIt never needs resizing.
BIt uses less memory always.
CIt can grow or shrink as needed.
DIt only stores integers.
✗ Incorrect
A dynamic stack can grow or shrink its storage array as elements are added or removed, unlike a fixed-size stack.
In Rust, which data structure is typically used to implement a dynamic stack?
AVec (vector)
BLinkedList
CHashMap
DArray fixed size
✗ Incorrect
Vec is a resizable array in Rust and is commonly used to implement dynamic stacks.
What happens internally when the stack's underlying array is full and a new element is pushed?
AThe array is resized to a larger capacity.
BThe new element is discarded.
CThe stack throws an error immediately.
DThe array shrinks.
✗ Incorrect
When full, the array is resized (usually doubled) to accommodate more elements.
Which operation removes the top element from the stack?
Apush
Bpop
Cpeek
Dinsert
✗ Incorrect
The pop operation removes and returns the top element from the stack.
Why is doubling the array size during resizing preferred over increasing by one element?
AIt causes errors.
BIt wastes more memory.
CIt slows down push operations.
DIt reduces the total number of resizes needed.
✗ Incorrect
Doubling reduces how often resizing happens, improving overall performance.
Explain how a dynamic stack using a resizable array works, including how it handles pushing and popping elements.
Think about what happens when the array is full or empty.
You got /4 concepts.
Describe why resizing the underlying array in a dynamic stack is important and how it affects performance.
Consider both adding and removing elements.
You got /4 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 does not require resizing or copying elements.
B. It can grow or shrink in size as needed during runtime.
C. It always has a fixed maximum size.
D. It uses less memory than a linked list stack.
Solution
Step 1: Understand fixed vs dynamic size
A fixed-size array stack has a set capacity that cannot change during runtime, limiting the number of elements it can hold.
Step 2: Analyze dynamic stack behavior
A dynamic stack using a resizable array can increase or decrease its capacity by resizing the underlying array, allowing it to handle more or fewer elements as needed.
Final Answer:
It can grow or shrink in size as needed during runtime. -> Option B
Quick Check:
Dynamic resizing = flexible size [OK]
Hint: Dynamic stack resizes array to fit elements [OK]
Common Mistakes:
Thinking dynamic stack has fixed size
Confusing linked list with array stack
Assuming no resizing needed
2. Which Rust code snippet correctly doubles the capacity of a vector when the stack is full?
easy
A. if stack.len() == stack.capacity() { stack.reserve(stack.capacity()); }
B. if stack.len() == stack.capacity() { stack.reserve(1); }
C. if stack.len() == stack.capacity() { stack.reserve(stack.len() / 2); }
D. if stack.len() == stack.capacity() { stack.reserve(0); }
Solution
Step 1: Understand vector capacity and reserve method
The vector's capacity is the total space allocated. To double capacity, we add capacity() more space using reserve(capacity()).
Step 2: Check each option's reserve argument
if stack.len() == stack.capacity() { stack.reserve(stack.capacity()); } reserves current capacity again, doubling it. Others reserve too little or zero, not doubling.
Final Answer:
if stack.len() == stack.capacity() { stack.reserve(stack.capacity()); } -> Option A
Quick Check:
Reserve current capacity to double [OK]
Hint: Reserve current capacity to double vector size [OK]
Common Mistakes:
Reserving fixed small number instead of doubling
Using zero in reserve causing no growth
Confusing len() with capacity()
3. What will be the output of the following Rust code implementing a dynamic stack?
A. reserve argument should be capacity(), not len()
B. reserve should be called after push, not before
C. No bug; code works correctly
D. push should be called only if capacity is not full
Solution
Step 1: Check the resizing logic
When self.data.len() == self.data.capacity(), the stack is full. reserve(self.data.len()) requests additional space for len() more elements. Since len() == capacity() at this point, it doubles the capacity.
Step 2: Verify correctness
reserve(self.data.capacity()) would be equivalent here. The code safely resizes before push, preventing issues, and works correctly. (Rust Vec::push auto-resizes too, but manual is valid.)
Final Answer:
No bug; code works correctly -> Option C
Quick Check:
len() == capacity() when called, so doubles [OK]
Hint: When full, reserve(len()) == reserve(capacity()), doubles size [OK]
Common Mistakes:
Reserving too little space (e.g., reserve(1))
Calling reserve after push causing potential panic
Assuming push auto-resizes without manual check
5. You want to implement a dynamic stack in Rust that shrinks its capacity when the number of elements falls below one-quarter of the capacity to save memory. Which approach correctly achieves this behavior?
hard
A. After pop, if len() < capacity() / 4, call data.shrink_to(len())
B. After pop, if len() < capacity() / 2, call data.reserve(len())
C. After pop, if len() < capacity() / 4, call data.reserve(capacity() / 2)
D. After pop, if len() < capacity() / 4, do nothing; shrinking is automatic
Solution
Step 1: Understand shrinking condition
We want to shrink when elements are less than 25% of capacity to save memory.
Step 2: Use shrink_to method correctly
Calling shrink_to(len()) reduces capacity to fit current elements, freeing unused space.
Step 3: Analyze options
After pop, if len() < capacity() / 4, call data.shrink_to(len()) correctly checks condition and calls shrink_to(len()). Others either use wrong methods or do nothing.
Final Answer:
After pop, if len() < capacity() / 4, call data.shrink_to(len()) -> Option A
Quick Check:
Shrink when quarter full using shrink_to(len()) [OK]
Hint: Shrink vector capacity with shrink_to(len()) when quarter full [OK]
Common Mistakes:
Using reserve instead of shrink_to for shrinking
Shrinking at half capacity instead of quarter
Assuming automatic shrinking without code
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.