What if your stack could magically grow whenever you need more space?
Why Dynamic Stack Using Resizable Array in DSA C++?
Imagine you have a stack of plates at home. You keep adding plates on top, but your shelf has a fixed size. When the shelf is full, you have to find a new bigger shelf and move all plates there manually.
Manually moving plates every time the shelf is full is tiring and wastes time. Similarly, a fixed-size stack runs out of space, and resizing it manually is error-prone and slow.
A dynamic stack using a resizable array automatically grows the shelf size when needed. It copies plates to a bigger shelf behind the scenes, so you never run out of space and don't have to do the hard work yourself.
int stack[5]; int top = -1; // Can't push if top == 4 // Need manual resize code
std::vector<int> stack;
stack.push_back(10); // Automatically resizesYou can keep adding items without worrying about space limits or manual resizing.
Apps like browsers use dynamic stacks to keep track of pages you visit, allowing unlimited back and forward navigation without fixed limits.
Manual fixed-size stacks limit how many items you can store.
Dynamic stacks resize automatically to hold more items.
This makes your programs flexible and easier to manage.
Practice
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 AQuick Check:
Dynamic resizing = automatic growth/shrink [OK]
- Thinking dynamic stack has fixed size
- Confusing memory use with linked list
- Assuming no resizing needed
Solution
Step 1: Identify array resizing in C++
For raw arrays,reallocis 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 usesreallocto resize preserving data. arr = arr * 2; is invalid syntax.Final Answer:
arr = (int*) realloc(arr, size * 2 * sizeof(int)); -> Option CQuick Check:
Raw array resize = realloc [OK]
- Using vector methods on raw arrays
- Creating new array without copying data
- Invalid pointer arithmetic
void push(int x) {
if (top == capacity) {
capacity *= 2;
arr = (int*) realloc(arr, capacity * sizeof(int));
}
arr[top++] = x;
}
// Initial capacity = 2, top = 0
push(10);
push(20);
push(30);
What is the value of capacity and the stack elements after these pushes?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 BQuick Check:
Capacity doubles when full, stack holds all pushed elements [OK]
- Not doubling capacity before adding new element
- Assuming capacity equals number of elements
- Ignoring realloc effect
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?Solution
Step 1: Analyze realloc usage
realloccan fail to allocate memory and return NULL, but the code does not check the return value before usingarr, 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 toarr. Handle the error case appropriately.Final Answer:
Bug: realloc is called without checking NULL; Fix: check realloc return. -> Option DQuick Check:
Always check realloc != NULL [OK]
- Ignoring minimum capacity limit
- Not decrementing top before access
- Not checking realloc NULL
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?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 AQuick Check:
Minimum capacity check prevents invalid resize [OK]
- Shrinking capacity below initial size
- Ignoring memory safety on realloc
- Assuming performance reason only
Ready to prove it?
Pass the test and this pattern moves unknown
