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 push and pop sequence?
Consider a stack implemented using an array with a maximum size of 3. What will be the printed stack after these operations?
DSA Typescript
class Stack { private items: number[] = []; private maxSize: number = 3; push(element: number): boolean { if (this.items.length === this.maxSize) return false; this.items.push(element); return true; } pop(): number | undefined { return this.items.pop(); } print(): string { return this.items.join(' -> ') + ' -> null'; } } const stack = new Stack(); stack.push(10); stack.push(20); stack.push(30); stack.push(40); // This push should fail stack.pop(); console.log(stack.print());
Attempts:
2 left
💡 Hint
Remember the stack size limit and that the last pushed element is popped first.
✗ Incorrect
The stack has a max size of 3. The first three pushes (10, 20, 30) succeed. The fourth push (40) fails because the stack is full. Then pop removes the last element (30). So the stack contains 10 and 20.
🧠 Conceptual
intermediateWhat happens when you pop from an empty stack implemented with an array?
Given a stack implemented using an array, what is the result of calling pop() when the stack is empty?
Attempts:
2 left
💡 Hint
Think about what pop() does when no elements exist.
✗ Incorrect
In array-based stacks, pop() returns undefined (or null) if the stack is empty, indicating no elements to remove.
❓ Predict Output
advancedWhat is the output after this sequence of push and pop operations?
Analyze the following TypeScript stack code and determine the printed stack state.
DSA Typescript
class Stack { private items: number[] = []; push(element: number) { this.items.push(element); } pop(): number | undefined { return this.items.pop(); } print(): string { return this.items.join(' -> ') + ' -> null'; } } const stack = new Stack(); stack.push(5); stack.push(10); stack.pop(); stack.push(15); stack.push(20); stack.pop(); console.log(stack.print());
Attempts:
2 left
💡 Hint
Track each push and pop carefully in order.
✗ Incorrect
Push 5, push 10, pop removes 10, push 15, push 20, pop removes 20. Remaining stack is 5 and 15.
🔧 Debug
advancedIdentify the error in this stack pop method implementation
What error will this TypeScript stack pop method cause when called on an empty stack?
DSA Typescript
class Stack { private items: number[] = []; pop(): number { return this.items.pop()!; } }
Attempts:
2 left
💡 Hint
Consider what happens when pop() returns undefined but non-null assertion is used.
✗ Incorrect
Using non-null assertion (!) forces TypeScript to treat pop() result as non-null, but if the array is empty, pop() returns undefined causing runtime error.
🚀 Application
expertHow many items remain after this complex push/pop sequence on a stack with max size 4?
Given a stack with max size 4, after this sequence of operations, how many items remain in the stack?
DSA Typescript
class Stack { private items: number[] = []; private maxSize: number = 4; push(element: number): boolean { if (this.items.length === this.maxSize) return false; this.items.push(element); return true; } pop(): number | undefined { return this.items.pop(); } size(): number { return this.items.length; } } const stack = new Stack(); stack.push(1); stack.push(2); stack.push(3); stack.pop(); stack.push(4); stack.push(5); stack.push(6); // should fail stack.pop(); stack.pop(); stack.push(7);
Attempts:
2 left
💡 Hint
Track the stack size after each operation and remember the max size limit.
✗ Incorrect
Push 1,2,3 (size 3), pop removes 3 (size 2), push 4,5 (size 4), push 6 fails (size 4), pop removes 5 (size 3), pop removes 4 (size 2), push 7 (size 3). Final size is 3.
