A dynamic stack using a resizable array is a stack data structure that grows or shrinks its underlying array size automatically when elements are pushed or popped, allowing flexible storage without fixed size limits.
Click to reveal answer
beginner
How does the stack resize when it becomes full?
When the stack's array is full, it creates a new larger array (usually double the current size), copies all elements to the new array, and then continues to push new elements.
Click to reveal answer
intermediate
What happens when elements are popped and the stack is less than a quarter full?
The stack can shrink its array size to save memory, usually by halving the array size, but never below a minimum size to avoid frequent resizing.
Click to reveal answer
beginner
Why is dynamic resizing important for stacks implemented with arrays?
Dynamic resizing allows the stack to handle any number of elements efficiently without wasting memory or crashing due to fixed size limits, combining the speed of arrays with flexibility.
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) on average. Occasionally, resizing takes O(n) time, but amortized over many operations, the average remains O(1).
Click to reveal answer
What triggers the resizing of the array in a dynamic stack?
AWhen the array is empty
BWhen popping an element
CWhen the stack size is exactly half
DWhen the array is full during a push operation
✗ Incorrect
The array resizes when it is full and a new element needs to be pushed.
What is the usual factor by which the array size increases during resizing?
A2 times
B1.5 times
C3 times
DNo increase
✗ Incorrect
The array size usually doubles to reduce the frequency of resizing.
What is the main advantage of using a dynamic stack over a fixed-size stack?
AIt can grow and shrink as needed
BIt uses less memory always
CIt is slower
DIt cannot pop elements
✗ Incorrect
Dynamic stacks adjust size to fit the number of elements, unlike fixed-size stacks.
What is the amortized time complexity of push operation in a dynamic stack?
AO(log n)
BO(n)
CO(1)
DO(n^2)
✗ Incorrect
Although resizing takes O(n), amortized over many pushes, the average time is O(1).
When shrinking the array, what condition is commonly checked?
AStack size is zero
BStack size is less than one quarter of array capacity
CStack size is equal to array capacity
DStack size is more than array capacity
✗ Incorrect
Shrinking usually happens when the stack size falls below one quarter of the array capacity.
Explain how a dynamic stack using a resizable array manages memory when pushing and popping elements.
Think about growing and shrinking the array size.
You got /4 concepts.
Describe the average time complexity of push and pop operations in a dynamic stack and why it is efficient.
Consider how resizing affects many operations over time.
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 can grow or shrink automatically as elements are pushed or popped.
B. It uses less memory than a linked list stack.
C. It always has a fixed maximum size.
D. It does not require resizing when full.
Solution
Step 1: Understand fixed vs dynamic size
A fixed-size array stack has a set capacity and cannot grow beyond it, causing overflow if exceeded.
Step 2: Explain dynamic resizing
A dynamic stack using a resizable array automatically increases or decreases its size when needed, avoiding overflow and wasted space.
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 arrays resize automatically to fit elements [OK]
Common Mistakes:
Thinking dynamic stack has fixed size
Confusing memory use with linked list
Assuming no resizing needed
2. Which of the following is the correct syntax to double the size of the array when the stack is full in C++?
easy
A. arr = new int[size * 2];
B. arr.resize(size * 2);
C. arr = (int*) realloc(arr, size * 2 * sizeof(int));
D. arr = arr * 2;
Solution
Step 1: Identify array resizing in C++
For raw arrays, realloc is used to resize memory blocks dynamically.
Step 2: Check each option
arr = new int[size * 2]; creates a new array but loses old data. arr.resize(size * 2); is for std::vector, not raw arrays. arr = (int*) realloc(arr, size * 2 * sizeof(int)); correctly uses realloc to resize preserving data. arr = arr * 2; is invalid syntax.
What is the value of capacity and the stack elements after these pushes?
medium
A. capacity = 2, stack = [10, 20, 30]
B. capacity = 4, stack = [10, 20, 30]
C. capacity = 3, stack = [10, 20, 30]
D. capacity = 4, stack = [10, 20]
Solution
Step 1: Trace push calls and capacity changes
Initial capacity is 2. After pushing 10 and 20, top reaches 2 (full). On pushing 30, capacity doubles to 4, and 30 is added.
Step 2: Confirm final stack and capacity
Stack elements are [10, 20, 30], capacity is 4 after resizing.
Final Answer:
capacity = 4, stack = [10, 20, 30] -> Option B
Quick Check:
Capacity doubles when full, stack holds all pushed elements [OK]
Hint: Capacity doubles only when full before push [OK]
Common Mistakes:
Not doubling capacity before adding new element
Assuming capacity equals number of elements
Ignoring realloc effect
4. The following code snippet for pop operation in a dynamic stack has a bug:
int pop() {
if (top == 0) {
throw "Stack Underflow";
}
int val = arr[--top];
if (top < capacity / 4) {
capacity /= 2;
arr = (int*) realloc(arr, capacity * sizeof(int));
}
return val;
}
What is the bug and how to fix it?
medium
A. Bug: throw uses string literal; Fix: use std::exception instead.
B. Bug: pop does not decrease top; Fix: decrement top before accessing.
C. Bug: capacity can become zero; Fix: ensure capacity never goes below 1.
D. Bug: realloc is called without checking NULL; Fix: check realloc return.
Solution
Step 1: Analyze realloc usage
realloc can fail to allocate memory and return NULL, but the code does not check the return value before using arr, which can lead to crashes.
Step 2: Fix by checking realloc return
Store the result in a temporary pointer and check if it is NULL before assigning to arr. Handle the error case appropriately.
Final Answer:
Bug: realloc is called without checking NULL; Fix: check realloc return. -> Option D
Quick Check:
Always check realloc != NULL [OK]
Hint: Always check realloc return for NULL [OK]
Common Mistakes:
Ignoring minimum capacity limit
Not decrementing top before access
Not checking realloc NULL
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 an initial capacity of 4. Which of the following code snippets correctly implements the pop operation with this behavior?
int pop() {
if (top == 0) throw "Underflow";
int val = arr[--top];
if (top < capacity / 4 && capacity > 4) {
capacity /= 2;
arr = (int*) realloc(arr, capacity * sizeof(int));
}
return val;
}
What is the key reason for checking capacity > 4 before resizing?
hard
A. To prevent the capacity from shrinking below the initial minimum size, avoiding invalid memory access.
B. To ensure the stack always has space for at least four elements, improving performance.
C. To avoid calling realloc too frequently, which slows down the program.
D. To keep the capacity as a power of two for easier indexing.
Solution
Step 1: Understand capacity shrinking condition
The stack shrinks when elements are less than one-fourth capacity, but must not shrink below initial capacity 4.
Step 2: Explain importance of capacity > 4 check
This check prevents capacity from becoming less than 4, which avoids invalid memory allocation and potential errors.
Final Answer:
To prevent the capacity from shrinking below the initial minimum size, avoiding invalid memory access. -> Option A