Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
We push the given value 'val' onto the main stack.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' which breaks min tracking.
Using '==' which misses smaller values.
Using '>=' which is incorrect for minimum.
✗ Incorrect
We add val to min_stack if it is smaller or equal to the current minimum.
3fill in blank
hardFix 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'
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.
✗ Incorrect
We must pop from min_stack to keep stacks in sync.
4fill in blank
hardFill 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'
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.
✗ Incorrect
We return the last element of min_stack which is the current minimum.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The push uses '<=' to update min_stack, pop calls pop(), and getMin returns last element.