Bird
0
0
DSA Cprogramming~20 mins

Queue Implementation Using Array in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Queue Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Enqueue and Dequeue Operations
What is the printed state of the queue after these operations?
DSA C
typedef struct {
    int arr[5];
    int front;
    int rear;
} Queue;

void enqueue(Queue *q, int val) {
    if (q->rear == 4) return; // queue full
    q->rear++;
    q->arr[q->rear] = val;
}

int dequeue(Queue *q) {
    if (q->front > q->rear) return -1; // queue empty
    int val = q->arr[q->front];
    q->front++;
    return val;
}

int main() {
    Queue q = {.front = 0, .rear = -1};
    enqueue(&q, 10);
    enqueue(&q, 20);
    enqueue(&q, 30);
    dequeue(&q);
    enqueue(&q, 40);
    // Print queue from front to rear
    for (int i = q.front; i <= q.rear; i++) {
        printf("%d -> ", q.arr[i]);
    }
    printf("null\n");
    return 0;
}
A10 -> 20 -> 30 -> 40 -> null
B20 -> 30 -> 40 -> null
C30 -> 40 -> null
D20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that dequeue removes the front element and front index moves forward.
🧠 Conceptual
intermediate
1:30remaining
Queue Full Condition in Array Implementation
In a simple queue implemented using an array of size 5 with front and rear indices, which condition correctly checks if the queue is full?
Arear == size - 1
Bfront == rear
Cfront == 0 && rear == size - 1
Dfront > rear
Attempts:
2 left
💡 Hint
Think about where rear points when the array is full.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Dequeue Function
What error will occur when running this dequeue function on an empty queue?
DSA C
int dequeue(Queue *q) {
    if (q->front == q->rear) {
        return -1; // queue empty
    }
    int val = q->arr[q->front];
    q->front++;
    return val;
}
AReturns garbage value due to wrong empty check
BInfinite loop in main program
CCauses segmentation fault due to invalid access
DReturns -1 correctly when queue is empty
Attempts:
2 left
💡 Hint
Check how front and rear are initialized and how empty is detected.
Predict Output
advanced
2:30remaining
Output After Multiple Enqueue and Dequeue
What is the printed queue state after these operations?
DSA C
typedef struct {
    int arr[4];
    int front;
    int rear;
} Queue;

void enqueue(Queue *q, int val) {
    if (q->rear == 3) return;
    q->rear++;
    q->arr[q->rear] = val;
}

int dequeue(Queue *q) {
    if (q->front > q->rear) return -1;
    int val = q->arr[q->front];
    q->front++;
    return val;
}

int main() {
    Queue q = {.front = 0, .rear = -1};
    enqueue(&q, 5);
    enqueue(&q, 10);
    enqueue(&q, 15);
    dequeue(&q);
    dequeue(&q);
    enqueue(&q, 20);
    enqueue(&q, 25);
    // Print queue
    for (int i = q.front; i <= q.rear; i++) {
        printf("%d -> ", q.arr[i]);
    }
    printf("null\n");
    return 0;
}
A5 -> 10 -> 15 -> 20 -> 25 -> null
B20 -> 25 -> null
C15 -> 20 -> null
D10 -> 15 -> 20 -> null
Attempts:
2 left
💡 Hint
Track front and rear indices carefully after each operation.
🧠 Conceptual
expert
3:00remaining
Limitation of Simple Array Queue and Its Solution
Why does a simple queue implemented with a fixed-size array and front/rear indices eventually fail to enqueue new elements even if some elements are dequeued? What is a common solution?
ABecause dequeue operation deletes elements physically shifting array; solution is to use linked list instead.
BBecause front and rear indices get reset to zero after each dequeue; solution is to increase array size dynamically.
CBecause enqueue operation overwrites front elements; solution is to use two arrays.
DBecause rear only moves forward and never wraps, the array space at front is wasted; solution is to use a circular queue.
Attempts:
2 left
💡 Hint
Think about how front and rear indices move and how array space is reused.