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 pushing elements on a stack?
Consider the following JavaScript code that pushes elements onto a stack implemented as an array. What will be the printed stack after all push operations?
DSA Javascript
const stack = []; stack.push(5); stack.push(10); stack.push(15); console.log(stack.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Remember that push adds elements to the end of the array, so the first pushed element is at the start of the array.
✗ Incorrect
In JavaScript, arrays used as stacks add elements at the end with push. So the stack array after pushing 5, 10, then 15 is [5, 10, 15]. Printing with join shows elements in order pushed.
❓ Predict Output
intermediateWhat is the stack content after pushing and popping?
Given the following code, what will be the printed stack after the operations?
DSA Javascript
const stack = []; stack.push(1); stack.push(2); stack.pop(); stack.push(3); console.log(stack.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Pop removes the last pushed element before the next push.
✗ Incorrect
Push 1 and 2 makes stack [1,2]. Pop removes 2, stack is [1]. Push 3 adds 3, stack is [1,3].
🧠 Conceptual
advancedHow does push operation affect stack size?
If a stack currently has 4 elements, what will be its size after pushing 3 more elements?
Attempts:
2 left
💡 Hint
Push adds one element each time.
✗ Incorrect
Each push adds one element, so 4 + 3 = 7 elements after pushing 3 more.
❓ Predict Output
advancedWhat error occurs when pushing on a non-array stack?
What error will this code produce?
DSA Javascript
const stack = null;
stack.push(1);Attempts:
2 left
💡 Hint
You cannot call methods on null.
✗ Incorrect
stack is null, so calling push on it causes a TypeError.
🚀 Application
expertTrace the stack content after multiple push and pop operations
Given the following code, what is the final printed stack?
DSA Javascript
const stack = []; stack.push('a'); stack.push('b'); stack.push('c'); stack.pop(); stack.push('d'); stack.pop(); stack.push('e'); console.log(stack.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Track each push and pop step by step.
✗ Incorrect
Push a,b,c: stack=[a,b,c]. Pop removes c: [a,b]. Push d: [a,b,d]. Pop removes d: [a,b]. Push e: [a,b,e].
