What if your stack could magically grow whenever you add more items, without crashing or manual work?
Why Dynamic Stack Using Resizable Array in DSA Typescript?
Imagine you have a stack of plates on your kitchen counter. You keep adding plates on top, but the stack can only hold a fixed number of plates. When you run out of space, you have to find another stack or rearrange everything manually.
Manually managing a fixed-size stack means you often run out of space or waste memory by reserving too much. You have to guess the size in advance, and resizing means copying all plates to a new stack, which is slow and error-prone.
A dynamic stack using a resizable array automatically grows when needed. It starts small and doubles its size when full, so you never run out of space. This makes adding and removing plates smooth and efficient without manual rearrangement.
class Stack { private items: number[] = new Array(5); private top = -1; push(item: number) { if (this.top === 4) throw new Error('Stack overflow'); this.items[++this.top] = item; } }
class DynamicStack { private items: number[] = []; private top = -1; push(item: number) { if (this.top + 1 === this.items.length) { this.resize(); } this.items[++this.top] = item; } private resize() { const newSize = this.items.length === 0 ? 1 : this.items.length * 2; const newItems = new Array(newSize); for (let i = 0; i <= this.top; i++) { newItems[i] = this.items[i]; } this.items = newItems; } }
It enables you to build flexible stacks that grow automatically, handling any number of elements without manual size limits or errors.
Think of a web browser's back button history. The number of pages you visit is unknown, so the stack of pages must grow dynamically as you browse.
Manual fixed-size stacks limit capacity and cause errors.
Dynamic stacks resize automatically to fit more elements.
This makes stack operations smooth and reliable for unknown data sizes.
Practice
Solution
Step 1: Understand fixed vs dynamic size
A fixed-size array stack has a set size and cannot grow beyond it, limiting capacity.Step 2: Understand dynamic resizing
A dynamic stack with resizable array can increase or decrease its size during runtime to fit more or fewer elements.Final Answer:
It can grow or shrink in size as needed during runtime. -> Option AQuick Check:
Dynamic resizing = flexible size [OK]
- Thinking dynamic stack has fixed max size
- Confusing memory use with linked list
- Assuming no resizing needed
Solution
Step 1: Understand resizing by copying
To resize, create a new larger array and copy existing elements into it.Step 2: Check each option
A uses spread but adds empty elements, not doubling size properly; B creates new array, copies elements, then replaces original array; C only changes length property but does not copy elements; D pushes empty elements, not resizing properly.Final Answer:
const newArray = new Array(this.array.length * 2); for(let i=0; i -> Option DQuick Check:
Copy elements to new larger array = B [OK]
- Only changing length without copying
- Using push to resize array
- Adding empty elements without copying
push(value: number) {
if (this.size === this.array.length) {
const newArray = new Array(this.array.length * 2);
for (let i = 0; i < this.array.length; i++) {
newArray[i] = this.array[i];
}
this.array = newArray;
}
this.array[this.size] = value;
this.size++;
}
If the initial array length is 2 and we push 3 elements: 10, 20, 30, what is the internal array state after these pushes?Solution
Step 1: Trace initial array and pushes
Initial array length is 2, so after pushing 10 and 20, array is full.Step 2: On pushing 30, array resizes to length 4 and copies elements 10, 20.
Then 30 is added at index 2, size becomes 3.Final Answer:
[10, 20, 30, undefined] -> Option AQuick Check:
Resize doubles length, elements copied, new slot added [OK]
- Forgetting to resize before adding third element
- Assuming array length stays 2
- Confusing undefined with 0
pop(): number | undefined {
if (this.size === 0) return undefined;
this.size--;
const value = this.array[this.size];
this.array[this.size] = undefined;
if (this.size > 0 && this.size === Math.floor(this.array.length / 4)) {
const newArray = new Array(Math.floor(this.array.length / 2));
for (let i = 0; i < this.size; i++) {
newArray[i] = this.array[i];
}
this.array = newArray;
}
return value;
}
What is the bug in this code?Solution
Step 1: Analyze order of operations
The code sets this.array[this.size] = undefined before copying elements to newArray.Step 2: Understand effect on copying
When copying, the popped element is already undefined, so data is lost if resizing happens now.Final Answer:
It sets popped element to undefined before copying, losing data. -> Option BQuick Check:
Clear after copying to avoid data loss [OK]
- Ignoring order of clearing and copying
- Shrinking array too often is not the main bug here
- Missing empty stack check is false here
Solution
Step 1: Understand resizing conditions
Doubling on push when full is standard. Halving on pop should avoid frequent resizing.Step 2: Avoid frequent resizing by checking size and minimum capacity
Only halve when size is less than one-quarter and array length is above initial capacity to prevent shrinking too small.Final Answer:
Resize array to double size on push when full, and halve size on pop only when size is less than one-quarter of array length and array length is greater than initial capacity. -> Option CQuick Check:
Resize carefully to avoid thrashing [OK]
- Shrinking too early causing many resizes
- Shrinking when size equals one-quarter causing thrashing
- Shrinking below initial capacity
Ready to prove it?
Pass the test and this pattern moves unknown
