Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the array that will store stack elements.
DSA C++
int stack[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names like 'size' or 'top' instead of a number.
Leaving the array size empty.
✗ Incorrect
The stack array size must be a fixed integer like 100 to store elements.
2fill in blank
mediumComplete the code to initialize the top of the stack.
DSA C++
int top = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing top to 0 which means stack has one element.
Using variable names instead of a number.
✗ Incorrect
Top is initialized to -1 to indicate the stack is empty.
3fill in blank
hardFix the error in the push function to add an element to the stack.
DSA C++
void push(int value) {
if(top == 99) {
cout << "Stack Overflow" << endl;
} else {
top = top [1] 1;
stack[top] = value;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' which decreases top.
Using '*' or '/' which are incorrect here.
✗ Incorrect
We increase top by 1 before adding the new element.
4fill in blank
hardFill both blanks to complete the pop function that removes the top element.
DSA C++
int pop() {
if(top == [1]) {
cout << "Stack Underflow" << endl;
return -1;
} else {
int value = stack[top];
top [2];
return value;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking if top is 0 instead of -1.
Using '+' instead of '--' to update top.
✗ Incorrect
Check if top is -1 for empty stack, then decrease top by 1 after popping.
5fill in blank
hardFill all three blanks to complete the isEmpty function that checks if the stack is empty.
DSA C++
bool isEmpty() {
if(top [1] [2]) {
return [3];
} else {
return false;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' for comparison.
Comparing top with 0 instead of -1.
Returning false when stack is empty.
✗ Incorrect
Stack is empty if top == -1, then return true.
