Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main characteristic of a queue implemented using an array?
easy
A. It follows First-In-First-Out (FIFO) order
B. It follows Last-In-First-Out (LIFO) order
C. It allows random access to elements
D. It stores elements in sorted order

Solution

  1. Step 1: Understand queue behavior

    A queue is a data structure where the first element added is the first one to be removed, which is FIFO.
  2. Step 2: Compare with options

    It follows First-In-First-Out (FIFO) order correctly states FIFO order. It follows Last-In-First-Out (LIFO) order is LIFO (stack), C allows random access (array), D stores sorted (not queue).
  3. Final Answer:

    It follows First-In-First-Out (FIFO) order -> Option A
  4. Quick Check:

    Queue = FIFO [OK]
Hint: Queue always removes the oldest element first [OK]
Common Mistakes:
  • Confusing queue with stack (LIFO)
  • Thinking queue allows random access
  • Assuming queue sorts elements automatically
2. Which of the following is the correct way to declare a queue array of size 5 in C?
easy
A. queue int[5];
B. int queue[5];
C. int queue();
D. int queue = new int[5];

Solution

  1. Step 1: Recall C array declaration syntax

    In C, arrays are declared as type name[size], e.g., int queue[5];
  2. Step 2: Check each option

    int queue[5]; matches correct syntax. queue int[5]; is invalid syntax. int queue(); declares a function, not an array. int queue = new int[5]; is Java-style syntax.
  3. Final Answer:

    int queue[5]; -> Option B
  4. Quick Check:

    C array declaration = int name[size] [OK]
Hint: C arrays use type name[size] syntax [OK]
Common Mistakes:
  • Using Java or other language syntax
  • Confusing function declaration with array
  • Missing semicolon or brackets
3. Consider this C code snippet for a queue implemented using an array of size 3:
int queue[3];
int front = -1, rear = -1;

// Enqueue 10
rear++;
queue[rear] = 10;
if(front == -1) front = 0;

// Enqueue 20
rear++;
queue[rear] = 20;

// Dequeue
int val = queue[front];
front++;

// Enqueue 30
rear++;
queue[rear] = 30;

// Current queue elements from front to rear?
medium
A. 30 -> null
B. 10 -> 20 -> 30 -> null
C. 20 -> 30 -> null
D. 10 -> 30 -> null

Solution

  1. Step 1: Trace enqueue and dequeue operations

    Initially front = -1, rear = -1. Enqueue 10: rear=0, queue[0]=10, front=0. Enqueue 20: rear=1, queue[1]=20. Dequeue: val=queue[front]=queue[0]=10, front=1. Enqueue 30: rear=2, queue[2]=30.
  2. Step 2: Identify current queue elements

    front=1, rear=2, so elements are queue[1]=20 and queue[2]=30. Hence queue is 20 -> 30 -> null.
  3. Final Answer:

    20 -> 30 -> null -> Option C
  4. Quick Check:

    After dequeue, front moves forward; queue = 20, 30 [OK]
Hint: After dequeue, front moves forward; queue shows elements from front to rear [OK]
Common Mistakes:
  • Not updating front after dequeue
  • Including dequeued elements in output
  • Confusing front and rear indices
4. Identify the error in this queue enqueue function using array in C:
void enqueue(int queue[], int *rear, int size, int value) {
    if (*rear == size) {
        printf("Queue is full\n");
        return;
    }
    (*rear)++;
    queue[*rear] = value;
}
medium
A. rear should be compared with size - 1, not size
B. rear should be incremented after inserting value
C. Function should return int instead of void
D. queue array should be dynamically allocated

Solution

  1. Step 1: Analyze the full condition

    The check if(*rear == size) is wrong. Array indices are 0 to size-1, so full when *rear == size-1 before incrementing. Current check allows overflow.
  2. Step 2: Compare options

    rear should be compared with size - 1, not size identifies the correct fix: compare with size-1, not size. Others are incorrect.
  3. Final Answer:

    rear should be compared with size - 1, not size -> Option A
  4. Quick Check:

    Full: *rear == size-1 [OK]
Hint: Full check: rear == size - 1 [OK]
Common Mistakes:
  • Comparing rear == size instead of size-1
  • Incorrect overflow condition
  • Ignoring zero-based indexing
5. You have a queue implemented using an array of size 4 with front = 1 and rear = 3. After dequeuing two elements and enqueuing one element, what will be the correct front and rear values if you implement a circular queue?
hard
A. front = 2, rear = 0
B. front = 3, rear = 1
C. front = 0, rear = 1
D. front = 3, rear = 0

Solution

  1. Step 1: Understand initial state and operations

    Initial: front=1, rear=3, size=4. Dequeue twice: front moves forward twice. Since circular, front = (1 + 2) % 4 = 3.
  2. Step 2: Enqueue one element

    rear moves forward by one circularly: rear = (3 + 1) % 4 = 0.
  3. Final Answer:

    front = 3, rear = 0 -> Option D
  4. Quick Check:

    Circular queue uses modulo for front and rear [OK]
Hint: Use modulo to wrap front and rear in circular queue [OK]
Common Mistakes:
  • Not using modulo for circular increment
  • Updating front or rear incorrectly
  • Confusing front and rear positions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard