Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a queue of integers named 'q'.
DSA C++
std::queue<int> [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stack' or 'list' as the queue name causes confusion.
Forgetting to specify the queue name.
✗ Incorrect
The queue is declared with the name 'q' to be used as the underlying container for the stack implementation.
2fill in blank
mediumComplete the code to push an element 'x' into the queue 'q'.
DSA C++
q.[1](x);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' instead of 'push' to add elements.
Using 'front' which only accesses the first element.
✗ Incorrect
To add an element to the queue, we use the 'push' method.
3fill in blank
hardFix the error in the code to remove the front element from the queue 'q'.
DSA C++
q.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' instead of 'pop' to remove elements.
Trying to assign or use 'front' as a removal method.
✗ Incorrect
To remove the front element from the queue, we use the 'pop' method.
4fill in blank
hardFill both blanks to rotate the queue elements after pushing a new element to simulate stack push.
DSA C++
for (int i = 0; i < [1] - 1; i++) { q.[2](q.front()); q.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' instead of 'push' inside the loop.
Incorrect loop count causing wrong rotation.
✗ Incorrect
We rotate the queue by pushing the front element to the back and popping it, repeating for size-1 times.
5fill in blank
hardFill both blanks to implement the 'top' method returning the top element of the stack.
DSA C++
int top() {
if (q.[1]()) return -1;
return q.[2]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop()' instead of 'front()' which removes the element.
Checking size() == 0 instead of using empty().
✗ Incorrect
We check if the queue is empty using 'empty()'. If not, we return the front element using 'front()'.
