Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto the stack.
DSA Java
public void push(int x) {
stack.[1](x);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push will remove an element instead of adding.
Using peek only views the top element without adding.
✗ Incorrect
The push method adds an element to the top of the stack.
2fill in blank
mediumComplete the code to get the minimum element from the min stack.
DSA Java
public int getMin() {
return minStack.[1]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes the minimum element, which is incorrect here.
Using push or empty does not retrieve the minimum.
✗ Incorrect
peek returns the top element without removing it, which is the current minimum.
3fill in blank
hardFix the error in the pop method to correctly update the min stack.
DSA Java
public void pop() {
if(stack.peek().equals(minStack.[1]())) {
minStack.pop();
}
stack.pop();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes the element, causing incorrect behavior.
Using push or empty is not suitable here.
✗ Incorrect
We compare the top of the main stack with the top of the min stack using peek, not pop.
4fill in blank
hardFill both blanks to correctly update the min stack when pushing a new element.
DSA Java
public void push(int x) {
stack.push(x);
if(minStack.isEmpty() || x [1] minStack.[2]()) {
minStack.push(x);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' reverses the logic.
Using pop removes the minimum element incorrectly.
✗ Incorrect
We check if x is less than or equal to the current minimum (peek) to update minStack.
5fill in blank
hardFill all three blanks to implement getMin method using minStack.
DSA Java
public int getMin() {
return minStack.[1]();
}
public void push(int x) {
stack.push(x);
if(minStack.isEmpty() || x [2] minStack.[3]()) {
minStack.push(x);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pop in getMin removes the minimum element.
Using '>' reverses the comparison logic.
✗ Incorrect
getMin uses peek to view the minimum; push compares x <= minStack.peek() to update minStack.
