Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Queue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Circular Queue Enqueue and Dequeue Operations
What is the printed state of the circular queue after the following operations?

Initialize queue with size 5.
Enqueue 10, 20, 30.
Dequeue one element.
Enqueue 40, 50, 60.
Print queue contents from front to rear.
DSA C
int size = 5;
int queue[5];
int front = -1, rear = -1;

// Enqueue function
void enqueue(int val) {
    if ((rear + 1) % size == front) return; // full
    if (front == -1) front = 0;
    rear = (rear + 1) % size;
    queue[rear] = val;
}

// Dequeue function
int dequeue() {
    if (front == -1) return -1; // empty
    int val = queue[front];
    if (front == rear) {
        front = rear = -1;
    } else {
        front = (front + 1) % size;
    }
    return val;
}

// Operations
enqueue(10);
enqueue(20);
enqueue(30);
dequeue();
enqueue(40);
enqueue(50);
enqueue(60);

// Print queue
int i = front;
while (true) {
    printf("%d ", queue[i]);
    if (i == rear) break;
    i = (i + 1) % size;
}
A30 40 50 60
B20 30 40 50 60
C10 20 30 40 50
D20 30 40 50
Attempts:
2 left
💡 Hint
Remember that dequeue removes the front element and enqueue adds at rear using modulo for wrap-around.
🧠 Conceptual
intermediate
1:30remaining
Capacity Check in Circular Queue
In a circular queue implemented with an array of size N, which condition correctly checks if the queue is full?
A(rear + 1) % N == front
Brear == N - 1
Cfront == rear
Dfront == -1
Attempts:
2 left
💡 Hint
Think about how rear and front wrap around in a circular queue.
🔧 Debug
advanced
1:30remaining
Identify the Bug in Circular Queue Dequeue
What error will occur when running this dequeue code snippet if the queue is empty?
DSA C
int dequeue() {
    if (front == -1) {
        printf("Queue is empty\n");
        return -1;
    }
    int val = queue[front];
    if (front == rear) {
        front = rear = -1;
    } else {
        front = (front + 1) % size;
    }
    return val;
}
ARuns correctly and returns front element
BPrints 'Queue is empty' and returns -1
CCompilation error due to missing return statement
DReturns garbage value because no return after empty check
Attempts:
2 left
💡 Hint
Check what happens after printing 'Queue is empty' when queue is empty.
📝 Syntax
advanced
1:30remaining
Correct Circular Queue Enqueue Syntax
Which option correctly updates rear and inserts value val in a circular queue of size N?
A
rear = (rear + 1) % N;
queue[rear] = val;
B
rear = rear + 1 % N;
queue[rear] = val;
C
rear = (rear + 1) / N;
queue[rear] = val;
D
rear = rear + 1 % N;
queue[val] = rear;
Attempts:
2 left
💡 Hint
Remember operator precedence and modulo usage for circular increment.
🚀 Application
expert
2:30remaining
Final Queue State After Mixed Operations
Given a circular queue of size 4, starting empty, perform these operations:
enqueue(1), enqueue(2), enqueue(3), dequeue(), enqueue(4), dequeue(), enqueue(5), enqueue(6).
What is the queue content from front to rear?
A4 5 6
B2 3 4 5
C3 4 5 6
D1 2 3 4
Attempts:
2 left
💡 Hint
Track front and rear carefully after each operation with wrap-around.

Practice

(1/5)
1. What is the main advantage of using a circular queue over a simple linear queue when implemented using an array?
easy
A. It automatically sorts the elements in the queue
B. It allows storing different data types in the same queue
C. It increases the maximum size of the queue beyond the array length
D. It efficiently uses the array space by reusing freed slots

Solution

  1. Step 1: Understand linear queue limitation

    In a linear queue using an array, once the rear reaches the end, no more elements can be added even if there are empty slots at the front.
  2. Step 2: Circular queue advantage

    A circular queue connects the end of the array back to the front, allowing reuse of empty slots freed by dequeuing, thus using space efficiently.
  3. Final Answer:

    It efficiently uses the array space by reusing freed slots -> Option D
  4. Quick Check:

    Circular queue reuses space = Advantage [OK]
Hint: Circular queue reuses space freed by dequeue [OK]
Common Mistakes:
  • Thinking circular queue increases array size
  • Believing it sorts elements automatically
  • Confusing data type flexibility with queue type
2. Which of the following is the correct condition to check if a circular queue is full when using an array of size n with front and rear indices?
easy
A. rear == (front + 1) % n
B. front == rear
C. front == (rear + 1) % n
D. rear == n - 1

Solution

  1. Step 1: Recall circular queue full condition

    The queue is full when the next position of rear (circularly) is front, meaning no space to insert new element.
  2. Step 2: Express condition mathematically

    This condition is: front == (rear + 1) % n, where % is modulo operator to wrap around.
  3. Final Answer:

    front == (rear + 1) % n -> Option C
  4. Quick Check:

    Full if front is next to rear circularly = Condition B [OK]
Hint: Full when front is just after rear modulo array size [OK]
Common Mistakes:
  • Using rear == (front + 1) % n (wrong direction)
  • Checking front == rear (means empty, not full)
  • Checking rear == n - 1 (linear queue condition)
3. Consider a circular queue of size 5 (indices 0 to 4). Initially, front = -1 and rear = -1. After the following operations:
enqueue(10), enqueue(20), enqueue(30), dequeue(), enqueue(40), enqueue(50), enqueue(60)
What will be the values of front and rear?
medium
A. front = 1, rear = 0
B. front = 0, rear = 4
C. front = 1, rear = 3
D. front = 2, rear = 1

Solution

  1. Step 1: Trace enqueue and dequeue operations

    Start: front = -1, rear = -1
    enqueue(10): front=0, rear=0
    enqueue(20): rear=1
    enqueue(30): rear=2
    dequeue(): front=1 (removed 10)
    enqueue(40): rear=3
    enqueue(50): rear=4
    enqueue(60): rear wraps to 0 (circular)
  2. Step 2: Final front and rear values

    After last enqueue, front=1, rear=0
  3. Final Answer:

    front = 1, rear = 0 -> Option A
  4. Quick Check:

    After wrap, rear=0 and front moved to 1 [OK]
Hint: Track front/rear step-by-step with wrap-around [OK]
Common Mistakes:
  • Not updating front after dequeue
  • Forgetting rear wraps to 0 after reaching end
  • Mixing front and rear indices
4. Given the following code snippet for dequeue operation in a circular queue, identify the error:
int dequeue() {
  if (front == -1) {
    printf("Queue is empty\n");
    return -1;
  }
  int data = queue[front];
  if (front == rear) {
    front = rear = -1;
  } else {
    front = front + 1;
  }
  return data;
}
medium
A. front should be updated as (front + 1) % size to wrap around
B. rear should be incremented instead of front
C. The check for empty queue is incorrect
D. Data should be returned after updating front

Solution

  1. Step 1: Understand circular queue front update

    In circular queue, front index must wrap around using modulo when incremented to stay within array bounds.
  2. Step 2: Identify error in front update

    Code uses front = front + 1; which can go beyond array size. Correct is front = (front + 1) % size.
  3. Final Answer:

    front should be updated as (front + 1) % size to wrap around -> Option A
  4. Quick Check:

    Use modulo for front increment in circular queue [OK]
Hint: Use modulo when incrementing front or rear [OK]
Common Mistakes:
  • Incrementing front without modulo wrap
  • Confusing front and rear updates
  • Returning data before updating front
5. You have a circular queue of size 6. After performing these operations:
enqueue(5), enqueue(10), enqueue(15), dequeue(), dequeue(), enqueue(20), enqueue(25), enqueue(30), enqueue(35)
What will be the queue content from front to rear?
hard
A. 5 -> 10 -> 15 -> 20 -> 25 -> 30 -> null
B. 15 -> 20 -> 25 -> 30 -> 35 -> null
C. 10 -> 15 -> 20 -> 25 -> 30 -> null
D. 20 -> 25 -> 30 -> 35 -> 5 -> null

Solution

  1. Step 1: Trace each operation step-by-step

    Start front = -1, rear = -1
    enqueue(5): front=0, rear=0, queue=[5]
    enqueue(10): rear=1, queue=[5,10]
    enqueue(15): rear=2, queue=[5,10,15]
    dequeue(): front=1 (removed 5)
    dequeue(): front=2 (removed 10)
    enqueue(20): rear=3, queue=[5,10,15,20]
    enqueue(25): rear=4, queue=[5,10,15,20,25]
    enqueue(30): rear=5, queue=[5,10,15,20,25,30]
    enqueue(35): rear wraps to 0, queue=[35,10,15,20,25,30]
  2. Step 2: Identify current front and rear and queue content

    front=2, rear=0
    Queue elements from front to rear circularly: 15 (index 2), 20 (3), 25 (4), 30 (5), 35 (0)
  3. Final Answer:

    15 -> 20 -> 25 -> 30 -> 35 -> null -> Option B
  4. Quick Check:

    Queue content from front to rear matches 15 -> 20 -> 25 -> 30 -> 35 -> null [OK]
Hint: Track front and rear indices carefully with wrap-around [OK]
Common Mistakes:
  • Ignoring wrap-around when rear reaches end
  • Listing elements linearly without circular order
  • Forgetting to update front after dequeues
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