Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a stack of integers.
DSA C++
std::stack<int> [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using reserved keywords like 'stack' as variable name.
Using names that are too long or unclear.
✗ Incorrect
The variable name 'stk' is commonly used for stack objects and matches the code style.
2fill in blank
mediumComplete the code to push the first element of the array into the stack.
DSA C++
stk.[1](arr[0]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' instead of 'push'.
Using 'insert' which is not a stack method.
✗ Incorrect
The 'push' method adds an element to the top of the stack.
3fill in blank
hardFix the error in the while loop condition to check if the stack is not empty.
DSA C++
while (!stk.[1]()) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size()' which returns a number, not a boolean.
Using 'top()' which returns the top element, not a boolean.
✗ Incorrect
The 'empty()' method returns true if the stack is empty, so '!empty()' checks if it is not empty.
4fill in blank
hardFill both blanks to complete the if condition that compares the current array element with the top of the stack.
DSA C++
if (arr[i] [1] stk.[2]()) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>'.
Using 'empty()' instead of 'top()'.
✗ Incorrect
We compare if the current element is greater than the top element of the stack using '>' and 'top()'.
5fill in blank
hardFill all three blanks to correctly assign the next greater element and update the stack.
DSA C++
result[stk.[1]()] = arr[i]; stk.[2](); stk.[3](i);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'empty()' instead of 'top()'.
Using 'push()' instead of 'pop()' to remove elements.
✗ Incorrect
We use 'top()' to get the index, 'pop()' to remove it, and 'push()' to add the current index.
