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 of this stack operation sequence?
Consider a stack initially empty. We perform these operations in order:
push(10), push(20), pop(), push(30), pop(), push(40), push(50), pop()
What is the content of the stack from top to bottom after these operations?
push(10), push(20), pop(), push(30), pop(), push(40), push(50), pop()
What is the content of the stack from top to bottom after these operations?
DSA Typescript
class Stack { private items: number[] = []; push(item: number) { this.items.push(item); } pop() { 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.pop(); stack.push(40); stack.push(50); stack.pop(); console.log(stack.print());
Attempts:
2 left
💡 Hint
Remember pop removes the top item, and push adds on top.
✗ Incorrect
After all operations, the stack has 10 at bottom and 40 on top. The last pop removed 50.
🧠 Conceptual
intermediateWhich property best describes a stack?
A stack is a data structure that follows which principle?
Attempts:
2 left
💡 Hint
Think about which element is removed first.
✗ Incorrect
Stacks remove the most recently added element first, so they follow Last In, First Out (LIFO).
🔧 Debug
advancedWhat error occurs when popping from an empty stack?
Given this TypeScript stack class, what happens if pop() is called on an empty stack?
DSA Typescript
class Stack { private items: number[] = []; push(item: number) { this.items.push(item); } pop() { return this.items.pop(); } } const stack = new Stack(); stack.pop();
Attempts:
2 left
💡 Hint
Check what Array.pop() returns when empty.
✗ Incorrect
Array.pop() returns undefined if the array is empty; no error is thrown.
🚀 Application
advancedHow to reverse a string using a stack?
Which code snippet correctly reverses the string 'hello' using a stack?
Attempts:
2 left
💡 Hint
Think about pushing all characters first, then popping to reverse order.
✗ Incorrect
Pushing all characters then popping them reverses the string due to LIFO.
❓ Predict Output
expertWhat is the final stack content after this complex sequence?
Given this sequence of stack operations, what is the final stack content from top to bottom?
push(1), push(2), push(3), pop(), push(4), pop(), pop(), push(5), push(6), pop()
push(1), push(2), push(3), pop(), push(4), pop(), pop(), push(5), push(6), pop()
DSA Typescript
class Stack { private items: number[] = []; push(item: number) { this.items.push(item); } pop() { return this.items.pop(); } print() { return this.items.slice().reverse().join(' -> ') + ' -> null'; } } const stack = new Stack(); stack.push(1); stack.push(2); stack.push(3); stack.pop(); stack.push(4); stack.pop(); stack.pop(); stack.push(5); stack.push(6); stack.pop(); console.log(stack.print());
Attempts:
2 left
💡 Hint
Trace each push and pop carefully to see what remains.
✗ Incorrect
After all operations, stack has 1 at bottom and 5 on top.
