Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after these stack operations?
Consider a stack with a maximum size of 3. We perform these operations in order:
push(10), push(20), pop(), push(30), push(40), push(50).
What is the printed stack content after these operations?
push(10), push(20), pop(), push(30), push(40), push(50).
What is the printed stack content after these operations?
DSA Javascript
class Stack { constructor(maxSize) { this.items = []; this.maxSize = maxSize; } push(element) { if (this.items.length < this.maxSize) { this.items.push(element); } } pop() { if (this.items.length > 0) { return this.items.pop(); } return null; } print() { console.log(this.items.join(' -> ') + ' -> null'); } } const stack = new Stack(3); stack.push(10); stack.push(20); stack.pop(); stack.push(30); stack.push(40); stack.push(50); stack.print();
Attempts:
2 left
💡 Hint
Remember the stack size limit and that push does nothing if full.
✗ Incorrect
After pushing 10 and 20, stack is [10,20]. Pop removes 20, stack is [10]. Push 30 and 40 add elements, stack is [10,30,40]. Push 50 is ignored because stack is full.
❓ Predict Output
intermediateWhat does this code print about stack emptiness?
Given this stack code, what is the output after these operations?
push(5), pop(), isEmpty(), push(7), isEmpty()
push(5), pop(), isEmpty(), push(7), isEmpty()
DSA Javascript
class Stack { constructor() { this.items = []; } push(element) { this.items.push(element); } pop() { if (this.items.length === 0) return null; return this.items.pop(); } isEmpty() { return this.items.length === 0; } } const stack = new Stack(); stack.push(5); stack.pop(); console.log(stack.isEmpty()); stack.push(7); console.log(stack.isEmpty());
Attempts:
2 left
💡 Hint
Check when the stack becomes empty after pop.
✗ Incorrect
After pushing 5 and popping it, stack is empty so isEmpty() returns true. After pushing 7, stack is not empty so isEmpty() returns false.
🧠 Conceptual
advancedWhich condition correctly checks if a stack is full?
Assuming a stack implemented with an array and a max size, which condition correctly checks if the stack is full?
Attempts:
2 left
💡 Hint
Full means no more elements can be added without exceeding max size.
✗ Incorrect
The stack is full exactly when its length equals the max size. Greater than max size is invalid state.
❓ Predict Output
advancedWhat error does this stack code raise?
What error occurs when calling pop() on an empty stack in this code?
DSA Javascript
class Stack { constructor() { this.items = []; } pop() { return this.items.pop().toString(); } } const stack = new Stack(); stack.pop();
Attempts:
2 left
💡 Hint
What does pop() return if the array is empty?
✗ Incorrect
pop() returns undefined on empty array, calling toString() on undefined causes TypeError.
🧠 Conceptual
expertHow to efficiently check if a stack is empty or full in O(1)?
You have a stack implemented with an array and a max size. Which approach ensures checking if the stack is empty or full takes constant time O(1)?
Attempts:
2 left
💡 Hint
Avoid scanning the whole array every time.
✗ Incorrect
Maintaining a count variable updated on each push/pop allows O(1) checks for empty/full by comparing count to zero or max size.
