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 simulation
What is the output of the following TypeScript code simulating a stack using an array directly?
DSA Typescript
const stack: number[] = []; stack.push(10); stack.push(20); stack.pop(); stack.push(30); console.log(stack);
Attempts:
2 left
💡 Hint
Remember that pop removes the last element pushed.
✗ Incorrect
The pop removes 20, so the stack after operations contains 10 and 30.
🧠 Conceptual
intermediateWhy use stack abstraction over direct array use?
Which of the following is the main reason to use a stack abstraction instead of directly using an array for stack operations?
Attempts:
2 left
💡 Hint
Think about controlling how the data structure is used.
✗ Incorrect
Stack abstraction hides the underlying array and only exposes push/pop to enforce Last-In-First-Out behavior.
❓ Predict Output
advancedOutput of stack class with direct array inside
What is the output of this TypeScript code implementing a stack class using an array internally?
DSA Typescript
class Stack { private items: number[] = []; push(item: number) { this.items.push(item); } pop(): number | undefined { return this.items.pop(); } peek(): number | undefined { return this.items[this.items.length - 1]; } } const s = new Stack(); s.push(5); s.push(15); console.log(s.peek()); s.pop(); console.log(s.peek());
Attempts:
2 left
💡 Hint
peek returns the last pushed item without removing it.
✗ Incorrect
After pushing 5 and 15, peek returns 15. After popping 15, peek returns 5.
🔧 Debug
advancedIdentify the error in stack implementation using array
What error will this TypeScript code produce when run?
DSA Typescript
class Stack { private items: number[] = []; push(item: number) { this.items.push(item); } pop(): number { return this.items.pop(); } } const s = new Stack(); s.pop();
Attempts:
2 left
💡 Hint
Check the return type of pop and what happens when popping empty array.
✗ Incorrect
pop() returns undefined if array is empty, but function signature expects number, causing a runtime type mismatch.
🚀 Application
expertStack abstraction benefits in complex algorithm
In which scenario is using a stack abstraction more beneficial than using an array directly?
Attempts:
2 left
💡 Hint
Think about algorithms that rely on strict LIFO order.
✗ Incorrect
Depth-first search uses a stack to track nodes; stack abstraction ensures correct push/pop usage and prevents errors.
