Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto the stack.
DSA C++
std::stack<int> s;
s.push([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() inside push() instead of a value.
Trying to use front() which is for queues.
Using empty() which returns a bool, not a value.
✗ Incorrect
To add an element to a stack, use push() with the value to add, like 10.
2fill in blank
mediumComplete the code to get the front element of the queue.
DSA C++
std::queue<int> q;
int frontElement = q.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes the element instead of accessing it.
Using top() which is a stack method.
Using push() which adds elements.
✗ Incorrect
To access the front element of a queue, use front() method.
3fill in blank
hardFix the error in the code to correctly remove the top element from the stack.
DSA C++
std::stack<int> s; s.push(5); s.[1]();
Attempts:
3 left
💡 Hint
Common Mistakes
Using front() which is a queue method.
Using remove() which does not exist for stack.
Using dequeue() which is for queues.
✗ Incorrect
To remove the top element from a stack, use pop() method.
4fill in blank
hardFill both blanks to create a queue and add an element to it.
DSA C++
std::queue<int> [1]; [2].push(20);
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in declaration and push.
Using s or stack which are usually for stacks, not queues.
Using queue as variable name which is allowed but inconsistent here.
✗ Incorrect
We declare a queue named q and then use q.push(20) to add element 20.
5fill in blank
hardFill all three blanks to create a stack, push an element, and get the top element.
DSA C++
std::stack<int> [1]; [1].[2](15); int topElement = [1].top();
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of push() to add elements.
Using enqueue() which is not a stack method.
Using different variable names inconsistently.
✗ Incorrect
We create a stack named st, push 15 onto it, then get the top element with top().
