Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element into the input stack.
DSA C#
public void Enqueue(int x) {
inputStack.[1](x);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop instead of Push to add elements.
Using Peek which only views the top element.
✗ Incorrect
To add an element to the queue, we push it onto the input stack using Push method.
2fill in blank
mediumComplete the code to check if the output stack is empty before transferring elements.
DSA C#
if (outputStack.[1] == 0) { while (inputStack.Count > 0) { outputStack.Push(inputStack.Pop()); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Peek instead of Count to check emptiness.
Using a non-existent property like Empty.
✗ Incorrect
We check the Count property to see if the output stack is empty (Count == 0).
3fill in blank
hardFix the error in the Dequeue method to return the correct element.
DSA C#
public int Dequeue() {
if (outputStack.Count == 0) {
while (inputStack.Count > 0) {
outputStack.Push(inputStack.Pop());
}
}
return outputStack.[1]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Peek which does not remove the element.
Using Push or Count which are incorrect here.
✗ Incorrect
Dequeue removes and returns the front element, so we use Pop on outputStack.
4fill in blank
hardFill both blanks to implement the Peek method correctly.
DSA C#
public int Peek() {
if (outputStack.[1] == 0) {
while (inputStack.Count > 0) {
outputStack.[2](inputStack.Pop());
}
}
return outputStack.Peek();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Peek instead of Count to check emptiness.
Using Pop instead of Push to add elements.
✗ Incorrect
We check if outputStack is empty by Count, then transfer elements using Push.
5fill in blank
hardFill all three blanks to implement the IsEmpty method correctly.
DSA C#
public bool IsEmpty() {
return inputStack.[1] == 0 && outputStack.[2] == 0;
}
// Usage example:
// bool empty = queue.[3]();Attempts:
3 left
💡 Hint
Common Mistakes
Using Peek instead of Count to check emptiness.
Using a non-existent property or method.
✗ Incorrect
We check if both stacks have zero Count to confirm the queue is empty, then call IsEmpty method.
