Challenge - 5 Problems
Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of the stack after these operations?
Consider a stack implemented using an array with the following operations:
push(10), push(20), pop(), push(30), push(40), pop()
What is the printed state of the stack (top to bottom) after these operations?
push(10), push(20), pop(), push(30), push(40), pop()
What is the printed state of the stack (top to bottom) after these operations?
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(); } print() { return this.items.slice().reverse().join(' -> ') + ' -> null'; } } const stack = new Stack(); stack.push(10); stack.push(20); stack.pop(); stack.push(30); stack.push(40); stack.pop(); console.log(stack.print());
Attempts:
2 left
💡 Hint
Remember pop removes the top element from the stack.
✗ Incorrect
After pushing 10 and 20, stack is [10,20]. Pop removes 20. Then push 30 and 40, stack is [10,30,40]. Pop removes 40. Final stack is [10,30]. Printed top to bottom is 30 -> 10 -> null.
🧠 Conceptual
intermediateWhich operation is NOT typical for a stack?
Stacks usually support push, pop, and peek operations. Which of the following operations is NOT a standard stack operation?
Attempts:
2 left
💡 Hint
Think about which operation is typical for queues, not stacks.
✗ Incorrect
enqueue is an operation used in queues to add elements at the end, not in stacks.
🔧 Debug
advancedWhat error does this stack implementation cause?
Consider this stack pop method:
pop() {
return this.items.pop();
}
What error or issue can occur if pop is called on an empty stack?
pop() {
return this.items.pop();
}
What error or issue can occur if pop is called on an empty stack?
DSA Javascript
class Stack { constructor() { this.items = []; } pop() { return this.items.pop(); } }
Attempts:
2 left
💡 Hint
What does JavaScript's array pop() return when empty?
✗ Incorrect
Array pop() returns undefined if the array is empty; no error is thrown.
❓ Predict Output
advancedWhat is the output after pushing and popping multiple elements?
Given the following code, what is the printed stack state (top to bottom)?
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(); } print() { return this.items.slice().reverse().join(' -> ') + ' -> null'; } } const stack = new Stack(); stack.push(5); stack.push(15); stack.push(25); stack.pop(); stack.push(35); stack.pop(); stack.pop(); console.log(stack.print());
Attempts:
2 left
💡 Hint
Track each push and pop carefully.
✗ Incorrect
Push 5,15,25: stack=[5,15,25]. Pop removes 25: stack=[5,15]. Push 35: stack=[5,15,35]. Pop removes 35: stack=[5,15]. Pop removes 15: stack=[5]. Printed top to bottom: 5 -> null.
🚀 Application
expertHow many items remain after this sequence of stack operations?
A stack starts empty. The following operations occur:
push(1), push(2), push(3), pop(), push(4), pop(), pop(), push(5)
How many items are in the stack at the end?
push(1), push(2), push(3), pop(), push(4), pop(), pop(), push(5)
How many items are in the stack at the end?
Attempts:
2 left
💡 Hint
Count pushes and pops step by step.
✗ Incorrect
Operations: push 1 [1], push 2 [1,2], push 3 [1,2,3], pop removes 3 [1,2], push 4 [1,2,4], pop removes 4 [1,2], pop removes 2 [1], push 5 [1,5]. Final stack has 2 items.
