Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Direct Array Use as Stack
What is the printed output after running this JavaScript code that uses an array directly as a stack?
DSA Javascript
const stack = []; stack.push(10); stack.push(20); stack.pop(); stack.push(30); console.log(stack.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Remember that pop removes the last pushed item.
✗ Incorrect
The code pushes 10, then 20, then pops the last item (20), then pushes 30. So the stack has 10 and 30.
🧠 Conceptual
intermediateWhy Use Stack Abstraction Instead of Direct Array?
Which of the following is the main reason to use a stack abstraction instead of using an array directly as a stack?
Attempts:
2 left
💡 Hint
Think about controlling how data is accessed and changed.
✗ Incorrect
Stack abstraction hides the internal structure and only allows push/pop, preventing accidental misuse.
❓ Predict Output
advancedOutput of Stack Class vs Direct Array
What is the output of this code that uses a Stack class abstraction compared to direct array use?
DSA Javascript
class Stack { constructor() { this.items = []; } push(element) { this.items.push(element); } pop() { return this.items.pop(); } print() { console.log(this.items.join(' -> ') + ' -> null'); } } const stack = new Stack(); stack.push(5); stack.push(15); stack.pop(); stack.push(25); stack.print();
Attempts:
2 left
💡 Hint
Trace push and pop carefully inside the class.
✗ Incorrect
The stack pushes 5, then 15, pops 15, then pushes 25. So the stack has 5 and 25.
🔧 Debug
advancedIdentify the Error in Direct Array Stack Usage
What error will this code produce when using an array directly as a stack?
DSA Javascript
const stack = []; stack.push(1); stack.push(2); const popped = stack.pop(); console.log(popped); console.log(stack.pop()); console.log(stack.pop());
Attempts:
2 left
💡 Hint
What happens if you pop from an empty array?
✗ Incorrect
Popping from an empty array returns undefined, so the last console.log prints undefined.
🚀 Application
expertWhy Stack Abstraction Helps Prevent Bugs
Which scenario best shows how stack abstraction prevents bugs compared to direct array use?
Attempts:
2 left
💡 Hint
Think about controlling how data is changed and accessed.
✗ Incorrect
Using direct arrays, code can insert or remove elements anywhere, breaking stack rules. Abstraction limits operations to push/pop, preserving correct behavior.
