Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a stack array of size 5.
DSA C
int stack[[1]]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a size other than 5.
Forgetting to specify the size.
✗ Incorrect
The stack array size is 5 to hold 5 elements.
2fill in blank
mediumComplete the code to initialize the top of the stack to -1.
DSA C
int top = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting top to 0 which means stack has one element.
Using positive numbers initially.
✗ Incorrect
Top is set to -1 to indicate the stack is empty initially.
3fill in blank
hardFix the error in the push function to correctly add an element to the stack.
DSA C
void push(int stack[], int *top, int value) {
if (*top < 4) {
*top = *top [1] 1;
stack[*top] = value;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction which moves top backward.
Using multiplication or division which is incorrect here.
✗ Incorrect
We add 1 to top to move to the next empty position before inserting.
4fill in blank
hardFill both blanks to correctly pop an element from the stack.
DSA C
int pop(int stack[], int *top) {
int value = stack[*top];
*top = *top [1] 1;
return value;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition which moves top forward incorrectly.
Using multiplication or division which is invalid here.
✗ Incorrect
We subtract 1 from top to move down after popping the element.
5fill in blank
hardFill all three blanks to check if the stack is empty and print a message.
DSA C
if (top [1] [2]) { printf("Stack is [3]\n"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=='.
Comparing top with 0 instead of -1.
Printing wrong message.
✗ Incorrect
We check if top equals -1 to know stack is empty and print 'empty'.
