What if your stack could grow magically whenever it needs more space?
Why Dynamic Stack Using Resizable Array in DSA C#?
Imagine you have a stack of plates that keeps growing as more plates are added. You try to use a fixed-size box to hold them, but when the box is full, you have to find a bigger box and move all plates manually.
Using a fixed-size array for a stack means you must guess the maximum size in advance. If you guess too small, you run out of space and must create a new bigger array and copy all items, which is slow and error-prone.
A dynamic stack with a resizable array automatically grows the storage when needed. It starts small and doubles the size when full, so you never run out of space and avoid manual copying headaches.
int[] stack = new int[5]; // When full, create new array and copy manually int[] newStack = new int[10]; for(int i=0; i<5; i++) newStack[i] = stack[i]; stack = newStack;
if(count == stack.Length) { Array.Resize(ref stack, stack.Length * 2); }
You can push unlimited items onto the stack without worrying about size limits or manual resizing.
When building undo functionality in a text editor, the number of actions is unknown. A dynamic stack lets you store all actions safely without predefining limits.
Fixed-size stacks limit capacity and need manual resizing.
Dynamic stacks resize automatically to handle more items.
This makes stack operations smooth and error-free for unknown sizes.
Practice
Solution
Step 1: Understand fixed-size array stack limitation
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 AQuick Check:
Dynamic resizing = automatic growth/shrink [OK]
- Thinking dynamic stack uses less memory always
- Believing dynamic stack has fixed capacity
- Confusing dynamic stack with linked list stack
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 DQuick Check:
Array.Resize method resizes arrays properly [OK]
- Trying to set array.Length directly
- Creating new array without copying old data
- Using LINQ Concat for resizing inefficiently
DynamicStack stack = new DynamicStack(); stack.Push(10); stack.Push(20); stack.Push(30); stack.Pop(); stack.Push(40); stack.PrintStack();
What will be the printed output?
Solution
Step 1: Trace stack operations
Push 10, stack: 10
Push 20, stack: 20 -> 10
Push 30, stack: 30 -> 20 -> 10
Pop removes 30, stack: 20 -> 10
Push 40, stack: 40 -> 20 -> 10Step 2: Determine printed output
PrintStack prints from top to bottom ending with null: 40 -> 20 -> 10 -> nullFinal Answer:
40 -> 20 -> 10 -> null -> Option BQuick Check:
Stack top after operations = 40 [OK]
- Forgetting pop removes top element
- Printing stack in wrong order
- Including popped elements in output
public void Push(int item) {
if (top == array.Length) {
Array.Resize(ref array, array.Length * 2);
}
array[top] = item;
top++;
}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 AQuick Check:
Resize when full, assign then increment top [OK]
- Using wrong resize condition (off by one)
- Incrementing top before assignment
- Assuming top starts at 1 instead of 0
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 CQuick Check:
Shrink when quarter full using Array.Resize [OK]
- Shrinking too often causing overhead
- Shrinking to quarter size instead of half
- Not preserving data during resize
Ready to prove it?
Pass the test and this pattern moves unknown
