Challenge - 5 Problems
Min Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of the min stack after these operations?
Consider a min stack that supports push, pop, top, and getMin operations. Starting with an empty stack, perform these operations:
push(5), push(3), push(7), pop(), getMin(), top()
What is the output of getMin() and top()?
push(5), push(3), push(7), pop(), getMin(), top()
What is the output of getMin() and top()?
DSA Javascript
class MinStack { constructor() { this.stack = []; this.minStack = []; } push(x) { this.stack.push(x); if (this.minStack.length === 0 || x <= this.minStack[this.minStack.length - 1]) { this.minStack.push(x); } } pop() { const val = this.stack.pop(); if (val === this.minStack[this.minStack.length - 1]) { this.minStack.pop(); } } top() { return this.stack[this.stack.length - 1]; } getMin() { return this.minStack[this.minStack.length - 1]; } } const minStack = new MinStack(); minStack.push(5); minStack.push(3); minStack.push(7); minStack.pop(); const min = minStack.getMin(); const top = minStack.top(); console.log({min, top});
Attempts:
2 left
💡 Hint
Remember that pop removes the last pushed element, and getMin returns the smallest element currently in the stack.
✗ Incorrect
After pushing 5, 3, and 7, the stack is [5,3,7]. Popping removes 7, so stack is [5,3]. The minimum is 3, and the top is 3.
🧠 Conceptual
intermediateWhy does Min Stack use an auxiliary stack?
In a Min Stack design, why do we use a second stack (minStack) alongside the main stack?
Attempts:
2 left
💡 Hint
Think about how to get the minimum element quickly without scanning the whole stack.
✗ Incorrect
The auxiliary stack keeps track of the minimum element at each push, so getMin can return the current minimum in O(1) time.
🔧 Debug
advancedWhat error occurs in this Min Stack implementation?
Identify the error in the following Min Stack pop method:
pop() {
const val = this.stack.pop();
if (val < this.minStack[this.minStack.length - 1]) {
this.minStack.pop();
}
}
What error or issue will this cause?
pop() {
const val = this.stack.pop();
if (val < this.minStack[this.minStack.length - 1]) {
this.minStack.pop();
}
}
What error or issue will this cause?
DSA Javascript
pop() {
const val = this.stack.pop();
if (val < this.minStack[this.minStack.length - 1]) {
this.minStack.pop();
}
}Attempts:
2 left
💡 Hint
Check the condition that triggers popping from minStack.
✗ Incorrect
The condition should check for equality (val === minStack top), not less than, to correctly update the minimum stack.
❓ Predict Output
advancedWhat is the output after these Min Stack operations?
Given the following operations on an empty Min Stack:
push(2), push(2), push(1), pop(), getMin(), pop(), getMin()
What are the outputs of the two getMin() calls?
push(2), push(2), push(1), pop(), getMin(), pop(), getMin()
What are the outputs of the two getMin() calls?
DSA Javascript
class MinStack { constructor() { this.stack = []; this.minStack = []; } push(x) { this.stack.push(x); if (this.minStack.length === 0 || x <= this.minStack[this.minStack.length - 1]) { this.minStack.push(x); } } pop() { const val = this.stack.pop(); if (val === this.minStack[this.minStack.length - 1]) { this.minStack.pop(); } } getMin() { return this.minStack[this.minStack.length - 1]; } } const minStack = new MinStack(); minStack.push(2); minStack.push(2); minStack.push(1); minStack.pop(); const firstMin = minStack.getMin(); minStack.pop(); const secondMin = minStack.getMin(); console.log({firstMin, secondMin});
Attempts:
2 left
💡 Hint
Remember minStack keeps track of minimums including duplicates.
✗ Incorrect
After popping 1, min is 2. After popping one 2, min remains 2 because another 2 is still in stack.
🧠 Conceptual
expertWhat is the time complexity of all Min Stack operations?
For a Min Stack implemented with two stacks (main stack and min stack), what is the time complexity of push, pop, top, and getMin operations?
Attempts:
2 left
💡 Hint
Think about how the auxiliary stack helps avoid scanning the entire stack.
✗ Incorrect
Using two stacks allows all operations to be done in constant time without scanning elements.
