Challenge - 5 Problems
Stack Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after pushing elements on the stack?
Consider the following TypeScript code that pushes elements onto a stack. What will be the printed stack after all push operations?
DSA Typescript
class Stack { private items: number[] = []; push(element: number) { this.items.push(element); } print() { console.log(this.items.join(' -> ') + ' -> null'); } } const stack = new Stack(); stack.push(5); stack.push(10); stack.push(15); stack.print();
Attempts:
2 left
💡 Hint
Remember that push adds elements to the end of the array, so the first pushed element is at the bottom.
✗ Incorrect
The push method adds elements to the end of the array. So the order is 5, then 10, then 15. Printing shows them in the order they were pushed.
❓ Predict Output
intermediateWhat is the stack content after pushing and popping?
Given the following TypeScript code, what will be the printed stack after the operations?
DSA Typescript
class Stack { private items: number[] = []; push(element: number) { this.items.push(element); } pop() { return this.items.pop(); } print() { console.log(this.items.join(' -> ') + ' -> null'); } } const stack = new Stack(); stack.push(1); stack.push(2); stack.pop(); stack.push(3); stack.print();
Attempts:
2 left
💡 Hint
Pop removes the last pushed element.
✗ Incorrect
Push 1, push 2, pop removes 2, then push 3. Stack is [1, 3].
🧠 Conceptual
advancedWhat happens internally when pushing on a stack implemented with an array?
When you push an element onto a stack implemented using an array, which of the following best describes what happens internally?
Attempts:
2 left
💡 Hint
Think about how push works on arrays.
✗ Incorrect
Push adds the new element at the end of the array, increasing its length by one. This is efficient and keeps the order.
🔧 Debug
advancedWhy does this push method fail to add elements correctly?
Look at this TypeScript stack push method. Why does it fail to add elements properly?
class Stack {
private items: number[] = [];
push(element: number) {
this.items[this.items.length] = element;
this.items.length = this.items.length + 1;
}
}
Attempts:
2 left
💡 Hint
Think about what happens when you increase array length manually.
✗ Incorrect
Assigning to items[items.length] adds the element correctly, but then increasing length manually adds empty slots, making the stack bigger than expected.
🚀 Application
expertAfter pushing elements, what is the top element of the stack?
Given this TypeScript stack implementation, after pushing 7, 14, and 21, what is the value of the top element?
DSA Typescript
class Stack { private items: number[] = []; push(element: number) { this.items.push(element); } top() { return this.items[this.items.length - 1]; } } const stack = new Stack(); stack.push(7); stack.push(14); stack.push(21); const topElement = stack.top();
Attempts:
2 left
💡 Hint
Top element is the last pushed element.
✗ Incorrect
The top method returns the last element in the array, which is the last pushed element, 21.
