Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to pop the top element from the stack.
DSA C++
stack.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using push instead of pop.
Using top() which only accesses the element but does not remove it.
✗ Incorrect
The pop() function removes the top element from the stack.
2fill in blank
mediumComplete the code to check if the stack is empty before popping.
DSA C++
if (!stack.[1]()) { stack.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using size() without comparing to zero.
Using top() which returns the top element, not a boolean.
✗ Incorrect
The empty() function returns true if the stack is empty.
3fill in blank
hardFix the error in the code to pop and print the top element safely.
DSA C++
if (!stack.empty()) { int topElement = stack.[1](); stack.pop(); std::cout << topElement << std::endl; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() to get the element, which is incorrect.
Not checking if the stack is empty before accessing top().
✗ Incorrect
Use top() to access the top element before popping it.
4fill in blank
hardFill both blanks to pop all elements from the stack and print them.
DSA C++
while (!stack.[1]()) { std::cout << stack.[2]() << std::endl; stack.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using size() without comparing to zero.
Using pop() to get the element instead of top().
✗ Incorrect
Use empty() to check if stack is not empty and top() to access the element.
5fill in blank
hardFill all three blanks to pop elements from the stack only if the top is greater than 10.
DSA C++
while (!stack.[1]()) { if (stack.[2]() [3] 10) { stack.pop(); } else { break; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the condition.
Not checking if stack is empty before accessing top().
✗ Incorrect
Use empty() to check stack, top() to access element, and > to compare.
