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 to the stack?
Consider a stack with a maximum size of 3. We push elements 10, 20, and 30 in order. What is the printed state of the stack after these operations?
DSA Typescript
class Stack { private items: number[] = []; private maxSize: number; constructor(size: number) { this.maxSize = size; } push(element: number): void { if (this.items.length < this.maxSize) { this.items.push(element); } } print(): void { console.log(this.items.join(' -> ') + ' -> null'); } } const stack = new Stack(3); stack.push(10); stack.push(20); stack.push(30); stack.print();
Attempts:
2 left
💡 Hint
Remember that push adds elements to the top, but print shows from bottom to top in this implementation.
✗ Incorrect
The stack stores elements in an array. Pushing 10, 20, 30 adds them in order. Printing joins them from bottom (first pushed) to top (last pushed). So output is '10 -> 20 -> 30 -> null'.
❓ Predict Output
intermediateWhat is the output when checking if the stack is empty?
Given a stack initialized with max size 2 and no elements pushed yet, what does the isEmpty() method return?
DSA Typescript
class Stack { private items: number[] = []; private maxSize: number; constructor(size: number) { this.maxSize = size; } isEmpty(): boolean { return this.items.length === 0; } } const stack = new Stack(2); console.log(stack.isEmpty());
Attempts:
2 left
💡 Hint
Check the length of the items array when no elements are pushed.
✗ Incorrect
The stack is empty initially, so items.length is 0. The isEmpty method returns true when length is 0.
❓ Predict Output
advancedWhat error occurs when pushing beyond stack capacity?
What happens if we try to push an element into a full stack of max size 2?
DSA Typescript
class Stack { private items: number[] = []; private maxSize: number; constructor(size: number) { this.maxSize = size; } push(element: number): void { if (this.items.length >= this.maxSize) { throw new Error('Stack overflow'); } this.items.push(element); } } const stack = new Stack(2); stack.push(1); stack.push(2); stack.push(3);
Attempts:
2 left
💡 Hint
Check the condition before pushing and what happens if the stack is full.
✗ Incorrect
The push method throws an error when trying to add an element beyond maxSize. So pushing the third element causes 'Stack overflow' error.
❓ Predict Output
advancedWhat is the output after popping elements from the stack?
Given a stack with elements [5, 10, 15] (5 at bottom, 15 at top), what is the printed state after popping two elements?
DSA Typescript
class Stack { private items: number[] = []; constructor(initialItems: number[]) { this.items = initialItems; } pop(): number | undefined { return this.items.pop(); } print(): void { console.log(this.items.join(' -> ') + ' -> null'); } } const stack = new Stack([5, 10, 15]); stack.pop(); stack.pop(); stack.print();
Attempts:
2 left
💡 Hint
Popping removes elements from the top (end) of the array.
✗ Incorrect
Popping twice removes 15 and then 10, leaving only 5. Printing shows '5 -> null'.
🧠 Conceptual
expertWhich condition correctly checks if the stack is full?
You have a stack implemented with an array and a maxSize property. Which condition correctly returns true when the stack is full?
Attempts:
2 left
💡 Hint
Stack is full when number of elements equals max size.
✗ Incorrect
The stack is full when the number of elements equals maxSize, so length === maxSize is true.
