Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element to the queue using stackPush.
DSA Java
public void enqueue(int x) {
stackPush.[1](x);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push to add elements.
Trying to peek or check empty instead of adding.
✗ Incorrect
To add an element to the queue, we push it onto stackPush using the push method.
2fill in blank
mediumComplete the code to move elements from stackPush to stackPop when stackPop is empty.
DSA Java
if (stackPop.[1]()) { while (!stackPush.empty()) { stackPop.push(stackPush.pop()); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() to check emptiness instead of empty().
Using peek() which only looks at the top element.
✗ Incorrect
We check if stackPop is empty using the empty() method before moving elements.
3fill in blank
hardFix the error in the dequeue method to return the correct element.
DSA Java
public int dequeue() {
if (stackPop.empty()) {
while (!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.[1]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using peek() which does not remove the element.
Using push() or empty() which are incorrect here.
✗ Incorrect
To remove and return the front element of the queue, we pop from stackPop.
4fill in blank
hardFill both blanks to implement the peek method that returns the front element without removing it.
DSA Java
public int peek() {
if (stackPop.[1]()) {
while (!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.[2]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes the element instead of peek().
Not checking if stackPop is empty before moving elements.
✗ Incorrect
We check if stackPop is empty using empty(), then return the top element using peek() without removing it.
5fill in blank
hardFill all three blanks to implement the isEmpty method that checks if the queue is empty.
DSA Java
public boolean isEmpty() {
return stackPush.[1]() && stackPop.[2]();
}
// Usage example:
// Queue is empty if both stacks are empty: stackPush.[3]() and stackPop.empty()Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() or peek() instead of empty() to check emptiness.
Checking only one stack instead of both.
✗ Incorrect
The queue is empty only if both stackPush and stackPop are empty, checked by empty() method.
