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 these stack operations?
Consider a stack implemented using a linked list. The following operations are performed in order:
push(10), push(20), pop(), push(30), push(40), pop()
What is the printed state of the stack after these operations?
push(10), push(20), pop(), push(30), push(40), pop()
What is the printed state of the stack after these operations?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class Stack { constructor() { this.top = null; } push(value) { const newNode = new Node(value); newNode.next = this.top; this.top = newNode; } pop() { if (!this.top) return null; const poppedValue = this.top.value; this.top = this.top.next; return poppedValue; } print() { let current = this.top; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const stack = new Stack(); stack.push(10); stack.push(20); stack.pop(); stack.push(30); stack.push(40); stack.pop(); stack.print();
Attempts:
2 left
💡 Hint
Remember that pop removes the top element from the stack.
✗ Incorrect
The operations push and pop modify the stack as follows:
push(10): stack is 10 -> null
push(20): stack is 20 -> 10 -> null
pop(): removes 20, stack is 10 -> null
push(30): stack is 30 -> 10 -> null
push(40): stack is 40 -> 30 -> 10 -> null
pop(): removes 40, stack is 30 -> 10 -> null
So the final stack is 30 -> 10 -> null.
push(10): stack is 10 -> null
push(20): stack is 20 -> 10 -> null
pop(): removes 20, stack is 10 -> null
push(30): stack is 30 -> 10 -> null
push(40): stack is 40 -> 30 -> 10 -> null
pop(): removes 40, stack is 30 -> 10 -> null
So the final stack is 30 -> 10 -> null.
🧠 Conceptual
intermediateWhat is the time complexity of push and pop in a linked list stack?
In a stack implemented using a linked list, what is the time complexity of the push and pop operations?
Attempts:
2 left
💡 Hint
Think about how many nodes you need to traverse for push and pop.
✗ Incorrect
In a linked list stack, push adds a new node at the head and pop removes the head node. Both operations only involve changing a few pointers and do not require traversing the list, so both are O(1).
🔧 Debug
advancedIdentify the error in this pop method
This pop method is part of a linked list stack implementation. What error will it cause when called on an empty stack?
pop() {
const poppedValue = this.top.value;
this.top = this.top.next;
return poppedValue;
}
pop() {
const poppedValue = this.top.value;
this.top = this.top.next;
return poppedValue;
}
DSA Javascript
class Stack { constructor() { this.top = null; } pop() { const poppedValue = this.top.value; this.top = this.top.next; return poppedValue; } }
Attempts:
2 left
💡 Hint
What happens if this.top is null?
✗ Incorrect
If the stack is empty, this.top is null. Trying to access this.top.value causes a TypeError because null has no properties.
❓ Predict Output
advancedWhat is the output of this stack print after mixed operations?
Given the following stack operations, what is the printed stack?
push(5), push(15), push(25), pop(), push(35), pop(), pop(), push(45), print()
push(5), push(15), push(25), pop(), push(35), pop(), pop(), push(45), print()
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class Stack { constructor() { this.top = null; } push(value) { const newNode = new Node(value); newNode.next = this.top; this.top = newNode; } pop() { if (!this.top) return null; const poppedValue = this.top.value; this.top = this.top.next; return poppedValue; } print() { let current = this.top; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const stack = new Stack(); stack.push(5); stack.push(15); stack.push(25); stack.pop(); stack.push(35); stack.pop(); stack.pop(); stack.push(45); stack.print();
Attempts:
2 left
💡 Hint
Track each push and pop carefully to see what remains.
✗ Incorrect
Operations step-by-step:
push(5): 5 -> null
push(15): 15 -> 5 -> null
push(25): 25 -> 15 -> 5 -> null
pop(): removes 25, stack is 15 -> 5 -> null
push(35): 35 -> 15 -> 5 -> null
pop(): removes 35, stack is 15 -> 5 -> null
pop(): removes 15, stack is 5 -> null
push(45): 45 -> 5 -> null
Final stack: 45 -> 5 -> null
push(5): 5 -> null
push(15): 15 -> 5 -> null
push(25): 25 -> 15 -> 5 -> null
pop(): removes 25, stack is 15 -> 5 -> null
push(35): 35 -> 15 -> 5 -> null
pop(): removes 35, stack is 15 -> 5 -> null
pop(): removes 15, stack is 5 -> null
push(45): 45 -> 5 -> null
Final stack: 45 -> 5 -> null
🧠 Conceptual
expertWhy is a linked list preferred over an array for stack implementation in some cases?
Which of the following is the best reason to choose a linked list over an array for implementing a stack?
Attempts:
2 left
💡 Hint
Think about memory allocation and resizing.
✗ Incorrect
Linked lists allocate memory dynamically for each element, so they do not require resizing. Arrays have fixed size and may need costly resizing operations when full.
