Bird
0
0
DSA Cprogramming~10 mins

Queue vs Stack When to Use Which in DSA C - Interactive Comparison 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 stack.

DSA C
void push(Stack* s, int value) {
    if (s->top < MAX_SIZE - 1) {
        s->top[1];
        s->items[s->top] = value;
    }
}
Drag options to blanks, or click blank then click option'
A+=
B++
C-=
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator which moves the top down.
Using assignment operators incorrectly.
2fill in blank
medium

Complete the code to dequeue an element from the queue.

DSA C
int dequeue(Queue* q) {
    if (q->front != q->rear) {
        int value = q->items[q->front];
        q->front[1];
        return value;
    }
    return -1; // Queue empty
}
Drag options to blanks, or click blank then click option'
A-=
B--
C+=
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator which moves front backward.
Not updating the front index.
3fill in blank
hard

Fix the error in the stack pop function to correctly return the top element.

DSA C
int pop(Stack* s) {
    if (s->top >= 0) {
        int value = s->items[s->top];
        s->top[1];
        return value;
    }
    return -1; // Stack empty
}
Drag options to blanks, or click blank then click option'
A--
B++
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing the top index which causes wrong element removal.
Not updating the top index.
4fill in blank
hard

Fill in the blank to check if the queue is empty and reset indices.

DSA C
int isEmpty(Queue* q) {
    if (q->front [1] q->rear) {
        q->front = 0;
        q->rear = 0;
        return 1;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using != which checks for non-empty.
Using > or < which are incorrect for empty check.
5fill in blank
hard

Fill all three blanks to create a stack and push an element.

DSA C
Stack* createStack() {
    Stack* s = (Stack*)malloc(sizeof([1]));
    s->top = [2];
    return s;
}

void push(Stack* s, int value) {
    if (s->top < MAX_SIZE - 1) {
        s->top[3];
        s->items[s->top] = value;
    }
}
Drag options to blanks, or click blank then click option'
AStack
B-1
C++
DQueue
Attempts:
3 left
💡 Hint
Common Mistakes
Allocating memory for wrong type.
Initializing top incorrectly.
Using wrong operator to update top.