Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Push and Pop Operations on Stack
What is the printed state of the stack after the following operations?
Push 10, Push 20, Push 30, Pop, Push 40
Push 10, Push 20, Push 30, Pop, Push 40
DSA C++
struct Node {
int data;
Node* next;
};
class Stack {
Node* top;
public:
Stack() : top(nullptr) {}
void push(int x) {
Node* temp = new Node();
temp->data = x;
temp->next = top;
top = temp;
}
void pop() {
if (top == nullptr) return;
Node* temp = top;
top = top->next;
delete temp;
}
void print() {
Node* temp = top;
while (temp != nullptr) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null" << std::endl;
}
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.pop();
s.push(40);
s.print();
return 0;
}Attempts:
2 left
💡 Hint
Remember that push adds to the top and pop removes from the top.
✗ Incorrect
Push adds elements to the top of the stack. After pushing 10, 20, 30, the stack is 30 -> 20 -> 10 -> null. Pop removes the top element (30), so stack becomes 20 -> 10 -> null. Then pushing 40 adds it on top: 40 -> 20 -> 10 -> null.
🧠 Conceptual
intermediateUnderstanding Stack Top Pointer
In a stack implemented using a linked list, what does the 'top' pointer represent?
Attempts:
2 left
💡 Hint
Think about where push and pop operations happen in a stack.
✗ Incorrect
The 'top' pointer always points to the most recently added element, which is the top of the stack. This allows push and pop operations to happen efficiently at the top.
🔧 Debug
advancedIdentify the Bug in Pop Method
What error will occur when running the pop method below if the stack is empty?
DSA C++
void pop() {
Node* temp = top;
top = top->next;
delete temp;
}Attempts:
2 left
💡 Hint
What happens if top is nullptr and you try to access top->next?
✗ Incorrect
If the stack is empty, top is nullptr. Accessing top->next causes a segmentation fault because you are dereferencing a null pointer.
❓ Predict Output
advancedOutput After Multiple Push and Pop
What is the printed stack after these operations?
Push 5, Push 15, Pop, Push 25, Push 35, Pop, Pop
Push 5, Push 15, Pop, Push 25, Push 35, Pop, Pop
DSA C++
struct Node {
int data;
Node* next;
};
class Stack {
Node* top;
public:
Stack() : top(nullptr) {}
void push(int x) {
Node* temp = new Node();
temp->data = x;
temp->next = top;
top = temp;
}
void pop() {
if (top == nullptr) return;
Node* temp = top;
top = top->next;
delete temp;
}
void print() {
Node* temp = top;
while (temp != nullptr) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null" << std::endl;
}
};
int main() {
Stack s;
s.push(5);
s.push(15);
s.pop();
s.push(25);
s.push(35);
s.pop();
s.pop();
s.print();
return 0;
}Attempts:
2 left
💡 Hint
Track each push and pop carefully from the top.
✗ Incorrect
Operations step-by-step:
- Push 5: stack is 5 -> null
- Push 15: 15 -> 5 -> null
- Pop: removes 15, stack is 5 -> null
- Push 25: 25 -> 5 -> null
- Push 35: 35 -> 25 -> 5 -> null
- Pop: removes 35, stack is 25 -> 5 -> null
- Pop: removes 25, stack is 5 -> null
🧠 Conceptual
expertMemory Usage in Stack Implemented with Linked List
Which statement about memory usage in a stack implemented using a linked list is true?
Attempts:
2 left
💡 Hint
Think about how linked lists allocate memory for nodes.
✗ Incorrect
Each node in a linked list is allocated dynamically on the heap when pushed. This allows the stack to grow as needed until system memory runs out.
