Challenge - 5 Problems
Dynamic Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Push Operations on Dynamic Stack
What is the printed state of the stack after pushing 1, 2, and 3 in order on a dynamic stack implemented with a resizable array?
DSA Javascript
class DynamicStack { constructor() { this.array = []; } push(value) { this.array.push(value); } print() { console.log(this.array.join(' -> ') + ' -> null'); } } const stack = new DynamicStack(); stack.push(1); stack.push(2); stack.push(3); stack.print();
Attempts:
2 left
💡 Hint
Remember that push adds elements to the end of the array.
✗ Incorrect
The push method adds elements to the end of the array. Printing the array shows elements in the order they were pushed.
❓ Predict Output
intermediateOutput After Pop Operation on Dynamic Stack
Given a dynamic stack with elements [5, 10, 15], what is the printed state after one pop operation?
DSA Javascript
class DynamicStack { constructor() { this.array = [5, 10, 15]; } pop() { return this.array.pop(); } print() { console.log(this.array.join(' -> ') + ' -> null'); } } const stack = new DynamicStack(); stack.pop(); stack.print();
Attempts:
2 left
💡 Hint
Pop removes the last element pushed.
✗ Incorrect
Pop removes the last element (15). Remaining elements are 5 and 10.
🔧 Debug
advancedIdentify the Error in Dynamic Stack Push Method
What error will occur when running this push method in a dynamic stack implemented with a resizable array?
DSA Javascript
class DynamicStack {
constructor() {
this.array = [];
}
push(value) {
this.array[this.array.length] = value;
this.array.length++;
}
}Attempts:
2 left
💡 Hint
Consider what happens when you manually increase array length after adding an element.
✗ Incorrect
The push method sets the element at array.length, then increments length again, causing the array length to increase twice and leaving undefined elements.
🧠 Conceptual
advancedWhy Use a Resizable Array for Dynamic Stack?
Which reason best explains why a resizable array is used to implement a dynamic stack?
Attempts:
2 left
💡 Hint
Think about what dynamic means in data structures.
✗ Incorrect
Resizable arrays allow the stack to adjust its size automatically, supporting dynamic growth and shrinkage.
🚀 Application
expertResulting Stack State After Mixed Operations
Given an empty dynamic stack, what is the printed state after these operations: push(4), push(7), pop(), push(9), push(1), pop(), pop()?
DSA Javascript
class DynamicStack { constructor() { this.array = []; } push(value) { this.array.push(value); } pop() { return this.array.pop(); } print() { console.log(this.array.join(' -> ') + ' -> null'); } } const stack = new DynamicStack(); stack.push(4); stack.push(7); stack.pop(); stack.push(9); stack.push(1); stack.pop(); stack.pop(); stack.print();
Attempts:
2 left
💡 Hint
Track each operation step by step on paper.
✗ Incorrect
Operations:
- push(4): [4]
- push(7): [4,7]
- pop(): removes 7 -> [4]
- push(9): [4,9]
- push(1): [4,9,1]
- pop(): removes 1 -> [4,9]
- pop(): removes 9 -> [4]
Final stack: [4]
