Challenge - 5 Problems
Min Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Min Stack Operations
What is the output of the following sequence of operations on a Min Stack?
Operations:
push(5), push(3), push(7), getMin(), pop(), getMin(), pop(), getMin()
Operations:
push(5), push(3), push(7), getMin(), pop(), getMin(), pop(), getMin()
DSA C
struct MinStack {
int stack[100];
int minStack[100];
int top;
int minTop;
};
void push(struct MinStack* ms, int x) {
ms->stack[++ms->top] = x;
if (ms->minTop == -1 || x <= ms->minStack[ms->minTop]) {
ms->minStack[++ms->minTop] = x;
}
}
void pop(struct MinStack* ms) {
if (ms->top == -1) return;
int val = ms->stack[ms->top--];
if (val == ms->minStack[ms->minTop]) {
ms->minTop--;
}
}
int getMin(struct MinStack* ms) {
if (ms->minTop == -1) return -1;
return ms->minStack[ms->minTop];
}
// Initial state: top = -1, minTop = -1
// Sequence:
// push(5), push(3), push(7), getMin(), pop(), getMin(), pop(), getMin()Attempts:
2 left
💡 Hint
Remember the min stack keeps track of the smallest values so far.
✗ Incorrect
After pushing 5, 3, 7, the minimum is 3. After popping 7, minimum remains 3. After popping 3, minimum becomes 5.
🧠 Conceptual
intermediate1:30remaining
Why Use a Secondary Stack in Min Stack Design?
Why do we use a secondary stack to keep track of minimum values in a Min Stack design?
Attempts:
2 left
💡 Hint
Think about how to get the minimum value quickly without scanning all elements.
✗ Incorrect
The secondary stack stores the minimum value at each point so getMin() can return in constant time.
🔧 Debug
advanced1:30remaining
Identify the Bug in Min Stack Pop Operation
What error will occur when running this pop function in a Min Stack if the stack is empty?
DSA C
void pop(struct MinStack* ms) {
int val = ms->stack[ms->top--];
if (val == ms->minStack[ms->minTop]) {
ms->minTop--;
}
}Attempts:
2 left
💡 Hint
What happens if top is -1 and you try to access stack[top]?
✗ Incorrect
If the stack is empty (top == -1), accessing stack[top--] reads invalid memory causing undefined behavior.
❓ Predict Output
advanced2:00remaining
Resulting Stack and Min Stack States
After performing these operations on an empty Min Stack:
push(2), push(2), push(1), pop(), getMin(), pop(), getMin()
What is the final printed output of getMin calls?
push(2), push(2), push(1), pop(), getMin(), pop(), getMin()
What is the final printed output of getMin calls?
DSA C
struct MinStack {
int stack[100];
int minStack[100];
int top;
int minTop;
};
// Assume push, pop, getMin as defined previously
// Operations:
// push(2), push(2), push(1), pop(), getMin(), pop(), getMin()Attempts:
2 left
💡 Hint
Remember that duplicate minimum values are stored separately in min stack.
✗ Incorrect
After pushing 2,2,1 and popping once, min is 2. After popping again, min remains 2 because of duplicate 2s.
🚀 Application
expert3:00remaining
Design Challenge: Min Stack with Constant Space
Which approach allows implementing a Min Stack that uses only one stack and constant extra space for tracking minimum values?
Attempts:
2 left
💡 Hint
Think about encoding minimum information within the stack elements themselves.
✗ Incorrect
Storing differences allows tracking minimum with only one stack and constant extra space by encoding minimum info.
