Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the stack is empty before popping.
DSA C
if (top == [1]) { printf("Stack Underflow\n"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if top == -1 which is incorrect for this stack implementation.
Using top == 1 which means one element is present, so pop is possible.
✗ Incorrect
The stack is empty when top is 0, so we check if top == 0 before popping.
2fill in blank
mediumComplete the code to pop the top element from the stack.
DSA C
int popped_element = stack[[1] - 1]; top--; printf("Popped element: %d\n", popped_element);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using top instead of top - 1 causes out-of-bounds access.
Decrementing top before accessing stack causes wrong element to be popped.
✗ Incorrect
The top element is at index top - 1, so we access stack[top - 1] before decrementing top.
3fill in blank
hardFix the error in the pop function to correctly update the top index.
DSA C
int pop() {
if (top == 0) {
printf("Stack Underflow\n");
return -1;
}
return stack[[1]];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using top-- causes accessing stack[top] which is out of bounds.
Using top + 1 or top - 1 incorrectly changes the index.
✗ Incorrect
Using --top decrements top first, then accesses the correct top element.
4fill in blank
hardFill both blanks to implement a safe pop function that returns the popped element or -1 if empty.
DSA C
int pop() {
if (top == [1]) {
return [2];
}
return stack[--top];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of -1 on empty stack.
Checking if top == 1 which is incorrect.
✗ Incorrect
If top is 0, stack is empty, so return -1 to indicate underflow.
5fill in blank
hardFill all three blanks to print the stack elements after popping one element.
DSA C
pop(); for (int i = [1]; i [2] 0; i[3]) { printf("%d ", stack[i]); } printf("\n");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ++ instead of -- causes infinite loop.
Starting loop at top instead of top - 1 misses last element.
✗ Incorrect
Start from top - 1, go down to 0, decrementing i to print all elements after pop.
