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 content of the stack from top to bottom after this operation sequence?
Consider a stack initially empty. We perform these operations in order:
push(5), push(10), pop(), push(7), pop(), push(3), push(8), pop()
What is the content of the stack from top to bottom after these operations?
push(5), push(10), pop(), push(7), pop(), push(3), push(8), pop()
What is the content of the stack from top to bottom after these operations?
DSA Javascript
const stack = []; stack.push(5); stack.push(10); stack.pop(); stack.push(7); stack.pop(); stack.push(3); stack.push(8); stack.pop(); console.log(stack);
Attempts:
2 left
💡 Hint
Remember that pop removes the top element and push adds on top.
✗ Incorrect
After the operations, the stack has 3 on top and 5 below it, so the array is [5, 3] but since top is last element, console.log shows [5,3].
🧠 Conceptual
intermediateWhich statement best describes the LIFO principle in stacks?
Choose the option that correctly explains the Last In First Out (LIFO) principle used in stacks.
Attempts:
2 left
💡 Hint
Think about how a stack of plates works.
✗ Incorrect
LIFO means the last element added is the first to be removed, so elements come out in reverse order of insertion.
🔧 Debug
advancedWhat error does this stack code produce?
Look at this JavaScript stack code snippet and identify the error it causes when run.
DSA Javascript
const stack = []; stack.push(1); stack.push(2); stack.pop(); stack.pop(); stack.pop(); // Extra pop console.log(stack);
Attempts:
2 left
💡 Hint
What happens if you pop from an empty array in JavaScript?
✗ Incorrect
In JavaScript, popping from an empty array returns undefined and does not throw an error.
🚀 Application
advancedHow many items remain in the stack after this sequence?
A stack starts empty. We perform:
push(4), push(9), push(2), pop(), push(7), pop(), pop(), push(5)
How many items are in the stack now?
push(4), push(9), push(2), pop(), push(7), pop(), pop(), push(5)
How many items are in the stack now?
DSA Javascript
const stack = []; stack.push(4); stack.push(9); stack.push(2); stack.pop(); stack.push(7); stack.pop(); stack.pop(); stack.push(5); console.log(stack.length);
Attempts:
2 left
💡 Hint
Count pushes and pops carefully.
✗ Incorrect
After all operations, the stack has two items: 4 and 5.
📝 Syntax
expertWhich option correctly implements a stack push method in JavaScript?
Given a Stack class with an array named 'items', which push method is correct?
DSA Javascript
class Stack { constructor() { this.items = []; } push(element) { // Which option is correct here? } }
Attempts:
2 left
💡 Hint
Check JavaScript array methods for adding elements at the end.
✗ Incorrect
JavaScript arrays use push() to add elements at the end. add(), insert(), append() are not valid methods.
