Consider a dynamic stack implemented using a resizable array. Initially, the stack is empty.
Operations:
- push(10)
- push(20)
- pop()
- push(30)
- push(40)
- pop()
What is the printed stack content from top to bottom?
class DynamicStack { private arr: number[] = []; push(val: number) { this.arr.push(val); } pop(): number | undefined { return this.arr.pop(); } printStack() { console.log(this.arr.slice().reverse().join(' -> ') + ' -> null'); } } const stack = new DynamicStack(); stack.push(10); stack.push(20); stack.pop(); stack.push(30); stack.push(40); stack.pop(); stack.printStack();
Remember that pop removes the top element and push adds on top.
After the operations, the stack has 10 at bottom and 30 on top. So printing top to bottom is 30 -> 10 -> null.
In a dynamic stack implemented using a resizable array, what is the typical behavior when the internal array reaches its capacity and a new element is pushed?
Think about how dynamic arrays handle capacity limits.
Dynamic arrays typically double their size when full to accommodate more elements, copying existing elements to the new larger array.
Given this pop method in a dynamic stack, what error will it cause?
pop(): number {
if (this.arr.length === 0) {
return -1;
}
return this.arr.pop();
}Check the return type and what pop() returns when array is empty.
pop() returns undefined if array is empty, but method signature expects number, causing a TypeError in strict typing.
In a dynamic stack using a resizable array, which approach helps reduce memory usage when many elements are popped?
Think about resizing strategies to balance performance and memory.
Shrinking the array when usage is low frees memory while avoiding frequent resizing.
Given a dynamic stack with initial capacity 2, doubling capacity when full, and halving capacity when less than quarter full, what is the output of the capacity after these operations?
push(1) push(2) push(3) pop() pop() pop()
Print the capacity after each operation in order as a list.
class DynamicStack { private arr: number[] = new Array(2); private capacity: number = 2; private size: number = 0; push(val: number) { if (this.size === this.capacity) { this.capacity *= 2; const newArr = new Array(this.capacity); for (let i = 0; i < this.size; i++) { newArr[i] = this.arr[i]; } this.arr = newArr; } this.arr[this.size++] = val; } pop(): number | undefined { if (this.size === 0) return undefined; const val = this.arr[--this.size]; if (this.size > 0 && this.size < this.capacity / 4) { this.capacity = Math.floor(this.capacity / 2); const newArr = new Array(this.capacity); for (let i = 0; i < this.size; i++) { newArr[i] = this.arr[i]; } this.arr = newArr; } return val; } getCapacity() { return this.capacity; } } const stack = new DynamicStack(); const capacities = []; stack.push(1); capacities.push(stack.getCapacity()); stack.push(2); capacities.push(stack.getCapacity()); stack.push(3); capacities.push(stack.getCapacity()); stack.pop(); capacities.push(stack.getCapacity()); stack.pop(); capacities.push(stack.getCapacity()); stack.pop(); capacities.push(stack.getCapacity()); console.log(capacities);
Track capacity changes carefully after each push and pop.
Capacity doubles from 2 to 4 on third push. Pops reduce size triggering capacity halving from 4 to 2, then from 2 to 1.
