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
How does the stack resize when it becomes full?
When the stack's underlying array is full, it creates a new array with double the size, copies all elements to the new array, and then continues pushing new elements.
Click to reveal answer
intermediate
What happens when elements are popped and the array is mostly empty?
If the number of elements falls below a quarter of the array's capacity, the array size is halved to save memory, keeping the stack efficient.
Click to reveal answer
beginner
Show the basic operations of a dynamic stack using a resizable array.
Operations include Push (add element), Pop (remove top element), Peek (view top element), and Resize (adjust array size when needed).
Click to reveal answer
intermediate
Why is doubling the array size on resize a good strategy?
Doubling the size reduces the number of resizes needed, making push operations mostly fast (amortized O(1)) and balancing memory use and performance.
Click to reveal answer
What happens when you push an element onto a full dynamic stack array?
AThe array size doubles and elements are copied to the new array
BThe push operation fails
CThe array size halves
DThe element is discarded
✗ Incorrect
When the array is full, it doubles in size and copies existing elements to the new array before adding the new element.
When does the dynamic stack shrink its array size?
AWhen the stack is half full
BWhen the stack is empty
CWhen the number of elements is less than one quarter of the array size
DNever shrinks
✗ Incorrect
The array shrinks when the number of elements falls below one quarter of the array capacity to save memory.
What is the time complexity of push operation in a dynamic stack on average?
AO(n)
BO(n^2)
CO(log n)
DO(1) amortized
✗ Incorrect
Push is O(1) on average because resizing happens rarely, making the amortized time constant.
Which operation returns the top element without removing it?
APop
BPeek
CPush
DResize
✗ Incorrect
Peek returns the top element without removing it from the stack.
Why is it important to resize the array in a dynamic stack?
ATo balance memory use and performance
BTo keep the stack size fixed
CTo prevent stack overflow errors
DTo sort the stack elements
✗ Incorrect
Resizing balances memory use and performance by growing or shrinking the array as needed.
Explain how a dynamic stack using a resizable array manages its size during push and pop operations.
Think about what happens when the array is full or mostly empty.
You got /4 concepts.
Describe the main operations of a dynamic stack implemented with a resizable array and their purpose.
Focus on how elements are added, removed, viewed, and how the array changes size.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using a dynamic stack with a resizable array over a fixed-size array stack?
easy
A. It can grow or shrink automatically as elements are pushed or popped.
B. It uses less memory than a fixed-size array stack.
C. It always has a fixed capacity to avoid resizing overhead.
A fixed-size array stack has a set capacity and cannot grow beyond it, causing overflow if exceeded.
Step 2: Understand dynamic stack advantage
A dynamic stack resizes its array automatically when full, allowing more elements without overflow.
Final Answer:
It can grow or shrink automatically as elements are pushed or popped. -> Option A
Quick Check:
Dynamic resizing = automatic growth/shrink [OK]
Hint: Dynamic stacks resize arrays automatically when full [OK]
Common Mistakes:
Thinking dynamic stack uses less memory always
Believing dynamic stack has fixed capacity
Confusing dynamic stack with linked list stack
2. Which of the following is the correct way to double the size of the internal array in a dynamic stack in C#?
easy
A. array = new int[array.Length * 2];
B. array.Length = array.Length * 2;
C. array = array.Concat(array).ToArray();
D. Array.Resize(ref array, array.Length * 2);
Solution
Step 1: Understand array resizing in C#
Arrays in C# have fixed length; to resize, use Array.Resize(ref array, newSize) which copies old elements.
Step 2: Analyze options
Array.Resize(ref array, array.Length * 2); uses Array.Resize correctly. array = new int[array.Length * 2]; creates a new array but loses old data. array.Length = array.Length * 2; is invalid syntax. array = array.Concat(array).ToArray(); uses LINQ but is inefficient and not typical for stack resizing.
Final Answer:
Array.Resize(ref array, array.Length * 2); -> Option D
Quick Check:
Array.Resize method resizes arrays properly [OK]
Hint: Use Array.Resize(ref array, newSize) to resize arrays in C# [OK]
Common Mistakes:
Trying to set array.Length directly
Creating new array without copying old data
Using LINQ Concat for resizing inefficiently
3. Consider the following C# code for a dynamic stack using a resizable array:
B. The top variable should be incremented before assigning the item.
C. The condition should be top == array.Length to resize, but top starts at 1 instead of 0.
D. The condition should be top == array.Length - 1 to resize.
Solution
Step 1: Analyze resizing condition
top equals array.Length means array is full, so resizing is needed before adding new item.
Step 2: Check push logic
Assign item at array[top], then increment top. This correctly places item and moves top pointer.
Final Answer:
No bug; the code resizes and pushes correctly. -> Option A
Quick Check:
Resize when full, assign then increment top [OK]
Hint: Resize when top equals length; assign then increment top [OK]
Common Mistakes:
Using wrong resize condition (off by one)
Incrementing top before assignment
Assuming top starts at 1 instead of 0
5. You want to implement a dynamic stack that shrinks its internal array size by half when the number of elements falls below one-quarter of the array capacity. Which approach correctly handles this resizing to optimize memory without losing data?
hard
A. Resize array to array.Length * 2 after popping elements to avoid frequent resizing.
B. Resize array to array.Length / 4 whenever an element is popped.
C. After popping, check if top <= array.Length / 4, then resize array to array.Length / 2 using Array.Resize(ref array, newSize).
D. Never shrink the array to avoid complexity; only grow it when full.
Solution
Step 1: Understand shrinking condition
To save memory, shrink when usage is low: top ≤ 1/4 capacity triggers halving the array size.
Step 2: Apply resizing correctly
Use Array.Resize(ref array, array.Length / 2) to shrink safely after popping, preserving existing elements.
Final Answer:
After popping, check if top <= array.Length / 4, then resize array to array.Length / 2 using Array.Resize(ref array, newSize). -> Option C
Quick Check:
Shrink when quarter full using Array.Resize [OK]
Hint: Shrink array when usage ≤ 25% by halving size [OK]
Common Mistakes:
Shrinking too often causing overhead
Shrinking to quarter size instead of half
Not preserving data during resize
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.