Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto the stack.
DSA C#
public void Push(int x) {
stack.[1](x);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Add or Append which are not stack methods.
Using Insert which is for lists, not stacks.
✗ Incorrect
The correct method to add an element to a stack in C# is Push.
2fill in blank
mediumComplete the code to get the minimum element from the min stack.
DSA C#
public int GetMin() {
return minStack.[1]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop which removes the element.
Using Top which is not a C# Stack method.
✗ 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 C#
public void Pop() {
if(stack.Peek() == minStack.[1]()) {
minStack.Pop();
}
stack.Pop();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop which removes the element and causes errors.
Using Top which is not a valid Stack method in C#.
✗ Incorrect
We compare the top elements using Peek, not Pop, to avoid removing them prematurely.
4fill in blank
hardFill both blanks to correctly push elements and update the min stack.
DSA C#
public void Push(int x) {
stack.Push(x);
if(minStack.Count == 0 || x [1] minStack.[2]()) {
minStack.Push(x);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using >=
Using Pop instead of Peek.
✗ Incorrect
We push x to minStack if x is less than or equal to the current min (Peek).
5fill in blank
hardFill all three blanks to implement the MinStack class constructor and initialize stacks.
DSA C#
public MinStack() {
stack = new Stack<int>();
minStack = new [1]<int>();
[2] = 0;
[3] = 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of Stack for minStack.
Using size instead of count or vice versa inconsistently.
✗ Incorrect
We initialize minStack as a Stack and set count and size to 0.
