Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Min Stack Design in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Min Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Min Stack after push and pop operations

Consider a Min Stack that supports push, pop, and getMin operations. What is the output of the getMin calls after the following operations?

push(5)
push(3)
push(7)
pop()
getMin()
pop()
getMin()
DSA Python
class MinStack:
    def __init__(self):
        self.stack = []
        self.min_stack = []

    def push(self, val):
        self.stack.append(val)
        if not self.min_stack or val <= self.min_stack[-1]:
            self.min_stack.append(val)

    def pop(self):
        if self.stack:
            val = self.stack.pop()
            if val == self.min_stack[-1]:
                self.min_stack.pop()

    def getMin(self):
        return self.min_stack[-1] if self.min_stack else None

ms = MinStack()
ms.push(5)
ms.push(3)
ms.push(7)
ms.pop()
print(ms.getMin())
ms.pop()
print(ms.getMin())
A[5, 3]
B[3, 7]
C[7, 3]
D[3, 5]
Attempts:
2 left
💡 Hint

Remember the min stack keeps track of the smallest values so far.

🧠 Conceptual
intermediate
1:00remaining
Understanding Min Stack space complexity

What is the worst-case extra space complexity of a Min Stack that stores minimum values alongside the main stack?

AO(n), where n is the number of elements pushed
BO(1), constant extra space
CO(log n), logarithmic extra space
DO(n^2), quadratic extra space
Attempts:
2 left
💡 Hint

Consider the case when each new element is smaller than all previous ones.

🔧 Debug
advanced
1:30remaining
Identify the error in Min Stack pop method

What error will the following Min Stack pop method cause when popping from an empty stack?

def pop(self):
    val = self.stack.pop()
    if val == self.min_stack[-1]:
        self.min_stack.pop()
ANo error, works fine
BIndexError due to popping from empty list
CKeyError due to missing key
DTypeError due to wrong data type
Attempts:
2 left
💡 Hint

What happens if self.stack is empty and pop() is called?

❓ Predict Output
advanced
2:00remaining
Output of getMin after multiple push and pop

What is the output of the getMin calls after these operations?

push(2)
push(0)
push(3)
push(0)
getMin()
pop()
getMin()
pop()
getMin()
DSA Python
class MinStack:
    def __init__(self):
        self.stack = []
        self.min_stack = []

    def push(self, val):
        self.stack.append(val)
        if not self.min_stack or val <= self.min_stack[-1]:
            self.min_stack.append(val)

    def pop(self):
        if self.stack:
            val = self.stack.pop()
            if val == self.min_stack[-1]:
                self.min_stack.pop()

    def getMin(self):
        return self.min_stack[-1] if self.min_stack else None

ms = MinStack()
ms.push(2)
ms.push(0)
ms.push(3)
ms.push(0)
print(ms.getMin())
ms.pop()
print(ms.getMin())
ms.pop()
print(ms.getMin())
A[0, 0, 2]
B[0, 3, 2]
C[0, 0, 0]
D[2, 0, 0]
Attempts:
2 left
💡 Hint

Track the min stack carefully after each pop.

🚀 Application
expert
3:00remaining
Design a Min Stack with O(1) space for min tracking

You want to design a Min Stack that uses only one stack and stores the minimum value without extra space for a min stack. Which approach below correctly achieves this?

AStore differences between current value and min; update min when pushing and popping accordingly
BStore all values normally and scan stack to find min on each getMin call
CUse two stacks: one for values and one for min values
DStore values in a queue and track min separately
Attempts:
2 left
💡 Hint

Think about encoding min info within the stack values themselves.

Practice

(1/5)
1. What is the main purpose of a Min Stack data structure?
easy
A. To store only the minimum elements pushed so far
B. To sort elements in ascending order automatically
C. To retrieve the minimum element in constant time along with stack operations
D. To reverse the order of elements pushed into the stack

Solution

  1. Step 1: Understand the Min Stack concept

    A Min Stack is designed to support normal stack operations and also return the minimum element quickly.
  2. Step 2: Identify the key feature

    The key feature is retrieving the minimum element in constant time, not sorting or reversing.
  3. Final Answer:

    To retrieve the minimum element in constant time along with stack operations -> Option C
  4. Quick Check:

    Min Stack = fast minimum retrieval [OK]
Hint: Min Stack always tracks minimum for O(1) retrieval [OK]
Common Mistakes:
  • Thinking Min Stack sorts elements
  • Assuming it stores only minimum elements
  • Confusing with reversing stack order
2. Which of the following is the correct way to initialize a Min Stack class in Python?
easy
A. class MinStack: def __init__(self): stack = [] min_stack = []
B. class MinStack: def __init__(self): self = []
C. class MinStack: def __init__(self): self.stack = None self.min_stack = None
D. class MinStack: def __init__(self): self.stack = [] self.min_stack = []

Solution

  1. Step 1: Check correct attribute initialization

    Attributes must be initialized with self to be instance variables.
  2. Step 2: Verify list initialization

    Both stack and min_stack should be empty lists initialized with self.
  3. Final Answer:

    class MinStack:\n def __init__(self):\n self.stack = []\n self.min_stack = [] -> Option D
  4. Quick Check:

    Use self for instance variables [OK]
Hint: Always use self to define instance variables in __init__ [OK]
Common Mistakes:
  • Not using self for attributes
  • Initializing attributes as None instead of lists
  • Assigning self to a list directly
3. What will be the output of the following code snippet?
min_stack = MinStack()
min_stack.push(3)
min_stack.push(5)
min_stack.push(2)
min_stack.push(1)
min_stack.pop()
print(min_stack.get_min())
medium
A. 2
B. 1
C. 3
D. 5

Solution

  1. Step 1: Trace push operations and min tracking

    Push 3 (min=3), push 5 (min=3), push 2 (min=2), push 1 (min=1).
  2. Step 2: Pop removes 1, min updates to 2

    After popping 1, the minimum is the next smallest, which is 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Pop removes min 1, next min = 2 [OK]
Hint: Pop min, next min is previous min in min stack [OK]
Common Mistakes:
  • Assuming min stays 1 after pop
  • Confusing push order with min updates
  • Ignoring min stack updates on pop
4. Identify the bug in this Min Stack pop method and choose the correct fix:
def pop(self):
    if self.stack:
        self.stack.pop()
        self.min_stack.pop()
medium
A. No bug; code is correct
B. Pop from min_stack only if popped element equals current min
C. Pop from min_stack before popping from stack
D. Check if min_stack is empty before popping it

Solution

  1. Step 1: Understand pop logic in Min Stack

    Pop from min_stack only if the popped element is the current minimum to keep min_stack accurate.
  2. Step 2: Identify bug and fix

    Current code always pops min_stack, which can cause errors if min_stack is out of sync.
  3. Final Answer:

    Pop from min_stack only if popped element equals current min -> Option B
  4. Quick Check:

    Pop min_stack conditionally to keep min correct [OK]
Hint: Pop min_stack only if popped value equals current min [OK]
Common Mistakes:
  • Always popping min_stack without condition
  • Not checking min_stack emptiness
  • Changing pop order incorrectly
5. You want to design a Min Stack that supports push, pop, top, and get_min all in O(1) time. Which approach is best?
hard
A. Use two stacks: one for all elements, one for current minimums
B. Use a queue to store elements and a variable for min
C. Use one stack and scan all elements to find min on get_min
D. Use a linked list and traverse to find min on each get_min

Solution

  1. Step 1: Analyze time complexity requirements

    All operations must be O(1), so scanning or traversing is not allowed.
  2. Step 2: Choose data structure approach

    Using two stacks, one for elements and one for minimums, allows constant time min retrieval.
  3. Final Answer:

    Use two stacks: one for all elements, one for current minimums -> Option A
  4. Quick Check:

    Two stacks = O(1) min retrieval [OK]
Hint: Two stacks track elements and minimums for O(1) min [OK]
Common Mistakes:
  • Using scanning which is O(n)
  • Using queue which doesn't support stack operations
  • Using linked list traversal for min
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard