Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto the first stack.
DSA C
void enqueue(int x) {
stack1[top1++] = [1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable instead of x.
Trying to push onto stack2 instead of stack1.
✗ Incorrect
We push the value x onto stack1 at the current top1 index.
2fill in blank
mediumComplete the code to check if both stacks are empty (queue is empty).
DSA C
int isEmpty() {
return (top1 == 0 && [1] == 0);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking stack arrays instead of top indices.
Using top1 twice instead of top1 and top2.
✗ Incorrect
The queue is empty if both stack1 and stack2 are empty, so top2 must be zero.
3fill in blank
hardFix the error in the dequeue function to correctly transfer elements from stack1 to stack2.
DSA C
int dequeue() {
if (top2 == 0) {
while (top1 > 0) {
stack2[top2++] = [1];
top1--;
}
}
return stack2[--top2];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using top1 instead of top1-1 causes out-of-bounds access.
Copying from stack2 instead of stack1.
✗ Incorrect
We move the top element from stack1 (at top1-1) to stack2 and then decrease top1.
4fill in blank
hardFill both blanks to correctly initialize the queue and reset stack tops.
DSA C
void initQueue() {
top1 = [1];
top2 = [2];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting top indices to -1 which is not used in this implementation.
Using NULL which is not valid for integer indices.
✗ Incorrect
Both top1 and top2 start at 0 indicating empty stacks.
5fill in blank
hardFill all three blanks to correctly check if the queue is empty and return an error value if so.
DSA C
int dequeue() {
if (top1 == 0 && top2 == 0) {
return [1];
}
if (top2 == 0) {
while (top1 > 0) {
stack2[top2++] = stack1[[2]];
top1--;
}
}
return stack2[[3]];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of -1 for empty queue.
Using top1 or top2 directly instead of top-1 for indexing.
✗ Incorrect
Return -1 if empty; move from stack1[top1-1]; return stack2[top2-1].
