Bird
0
0
DSA Cprogramming~10 mins

Queue Using Two Stacks in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Atop1
Bstack2[top2]
Cx
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable instead of x.
Trying to push onto stack2 instead of stack1.
2fill in blank
medium

Complete 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'
Astack2
Btop1
Cstack1
Dtop2
Attempts:
3 left
💡 Hint
Common Mistakes
Checking stack arrays instead of top indices.
Using top1 twice instead of top1 and top2.
3fill in blank
hard

Fix 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'
Astack1[top1 - 1]
Bstack2[top2 - 1]
Cstack2[top2]
Dstack1[top1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using top1 instead of top1-1 causes out-of-bounds access.
Copying from stack2 instead of stack1.
4fill in blank
hard

Fill 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'
A0
B-1
C1
DNULL
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.
5fill in blank
hard

Fill 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'
A-1
Btop1 - 1
Ctop2 - 1
D0
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.