0
0
DSA Pythonprogramming~10 mins

Min Stack Design in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to push an element onto the stack.

DSA Python
def push(self, val):
    self.stack.append([1])
Drag options to blanks, or click blank then click option'
Aval
Bself.min_stack[-1]
Cself.stack[-1]
Dmin(val)
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.min_stack[-1] instead of val.
Trying to call min(val) which is incorrect.
Using self.stack[-1] which is the last element, not the new one.
2fill in blank
medium

Complete the code to update the min_stack when pushing a new value.

DSA Python
def push(self, val):
    self.stack.append(val)
    if not self.min_stack or val [1] self.min_stack[-1]:
        self.min_stack.append(val)
    else:
        self.min_stack.append(self.min_stack[-1])
Drag options to blanks, or click blank then click option'
A>
B>=
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' which breaks min tracking.
Using '==' which misses smaller values.
Using '>=' which is incorrect for minimum.
3fill in blank
hard

Fix the error in the pop method to correctly remove elements from both stacks.

DSA Python
def pop(self):
    if self.stack:
        self.stack.pop()
        [1]
Drag options to blanks, or click blank then click option'
Aself.min_stack.push()
Bself.min_stack.pop()
Cself.min_stack.append()
Dself.min_stack.remove()
Attempts:
3 left
💡 Hint
Common Mistakes
Using append() instead of pop().
Using push() which is not a list method.
Using remove() which removes by value, not last element.
4fill in blank
hard

Fill both blanks to return the current minimum element from the stack.

DSA Python
def getMin(self):
    if self.min_stack:
        return self.min_stack[1][2]
Drag options to blanks, or click blank then click option'
A[-1]
B[0]
C.pop()
D.append()
Attempts:
3 left
💡 Hint
Common Mistakes
Using [0] which returns the first element, not the minimum.
Using .pop() which removes the element.
Using .append() which adds an element.
5fill in blank
hard

Fill all three blanks to complete the MinStack class with push, pop, and getMin methods.

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 [1] self.min_stack[-1]:
            self.min_stack.append(val)
        else:
            self.min_stack.append(self.min_stack[-1])

    def pop(self):
        if self.stack:
            self.stack.pop()
            self.min_stack.[2]()

    def getMin(self):
        if self.min_stack:
            return self.min_stack[3]
Drag options to blanks, or click blank then click option'
A<=
Bpop
C[-1]
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' in push.
Using append() instead of pop() in pop method.
Using [0] or pop() in getMin which removes element.