Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto the stack.
DSA C
void push(int stack[], int *top, int value) {
stack[++[1]] = value;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'top' instead of '*top' causes incorrect pointer usage.
Assigning value before incrementing top.
✗ Incorrect
The top pointer is incremented first, then the value is assigned at that position.
2fill in blank
mediumComplete the code to pop an element from the stack.
DSA C
int pop(int stack[], int *top) {
if (*top == -1) return -1; // stack empty
return stack[[1]--];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using prefix decrement causes wrong element to be returned.
Using 'top' instead of '*top'.
✗ Incorrect
Return the current top element, then decrement the top pointer.
3fill in blank
hardFix the error in the stack overflow check condition.
DSA C
int isFull(int top, int maxSize) {
return [1] >= maxSize - 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*top' causes compilation error.
Comparing with maxSize instead of maxSize - 1.
✗ Incorrect
The top index is compared to maxSize - 1 to check if stack is full.
4fill in blank
hardFill both blanks to check if the stack is empty and return the top element safely.
DSA C
int safePop(int stack[], int *top) {
if (*top [1] -1) return -1;
return stack[[2]--];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '==' causes wrong empty check.
Using 'top' instead of '*top'.
✗ Incorrect
Check if top equals -1 to detect empty stack, then return element at *top and decrement.
5fill in blank
hardFill all three blanks to implement a function that returns the current top element without popping it.
DSA C
int peek(int stack[], int *top) {
if (*top [1] -1) return -1;
return stack[[2]];
// top remains [3]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '==' causes wrong empty check.
Modifying top in peek causes stack corruption.
✗ Incorrect
Check if stack is empty by comparing *top to -1, then return element at *top without changing top.
