Bird
Raised Fist0
DSA Goprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Queue Master
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 4.
Enqueue 10, 20, 30.
Dequeue one element.
Enqueue 40, 50.
Print queue contents from front to rear.
DSA Go
package main
import "fmt"
func main() {
  size := 4
  queue := make([]int, size)
  front, rear, count := 0, -1, 0

  enqueue := func(val int) bool {
    if count == size {
      return false
    }
    rear = (rear + 1) % size
    queue[rear] = val
    count++
    return true
  }

  dequeue := func() (int, bool) {
    if count == 0 {
      return 0, false
    }
    val := queue[front]
    front = (front + 1) % size
    count--
    return val, true
  }

  enqueue(10)
  enqueue(20)
  enqueue(30)
  dequeue()
  enqueue(40)
  enqueue(50)

  // Print queue contents
  for i := 0; i < count; i++ {
    idx := (front + i) % size
    fmt.Print(queue[idx])
    if i != count-1 {
      fmt.Print(" -> ")
    }
  }
  fmt.Print(" -> null")
}
A30 -> 40 -> 50 -> null
B10 -> 20 -> 30 -> 40 -> null
C20 -> 30 -> 40 -> 50 -> null
D20 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint
Remember that dequeue removes the front element and enqueue adds at rear with wrap-around.
❓ Predict Output
intermediate
2:00remaining
Result of Circular Queue Full Condition Check
Given a circular queue of size 5, after these operations:
Enqueue 1, 2, 3, 4, 5
Dequeue two elements
Enqueue 6, 7
What will be the value of 'count' and will the queue be full?
DSA Go
package main
import "fmt"
func main() {
  size := 5
  queue := make([]int, size)
  front, rear, count := 0, -1, 0

  enqueue := func(val int) bool {
    if count == size {
      return false
    }
    rear = (rear + 1) % size
    queue[rear] = val
    count++
    return true
  }

  dequeue := func() (int, bool) {
    if count == 0 {
      return 0, false
    }
    val := queue[front]
    front = (front + 1) % size
    count--
    return val, true
  }

  enqueue(1)
  enqueue(2)
  enqueue(3)
  enqueue(4)
  enqueue(5)
  dequeue()
  dequeue()
  enqueue(6)
  enqueue(7)

  fmt.Printf("count=%d, full=%t", count, count == size)
}
Acount=4, full=false
Bcount=5, full=false
Ccount=3, full=false
Dcount=5, full=true
Attempts:
2 left
💡 Hint
Count increases on enqueue and decreases on dequeue. Full means count equals size.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Circular Queue Dequeue Function
What error will occur when running this dequeue function in a circular queue implementation if the queue is empty?
DSA Go
func dequeue() int {
  if count == 0 {
    return -1
  }
  val := queue[front]
  front = (front + 1) % size
  count--
  return val
}
ANo error, returns -1 when empty
BPanic: index out of range when queue is empty
CReturns zero value instead of error
DCompilation error due to missing return type
Attempts:
2 left
💡 Hint
Check what happens when count is zero and dequeue is called.
🚀 Application
advanced
2:00remaining
Choosing Circular Queue Over Linear Queue
Why is a circular queue preferred over a linear queue when implemented using arrays?
ACircular queue allows infinite size without resizing.
BCircular queue efficiently uses all array spaces by wrapping around, avoiding wasted space after dequeues.
CCircular queue does not require front and rear pointers.
DCircular queue is easier to implement than linear queue.
Attempts:
2 left
💡 Hint
Think about what happens to array space after many dequeues in linear queue.
🧠 Conceptual
expert
2:00remaining
Maximum Number of Elements in Circular Queue
If a circular queue is implemented using an array of size N, what is the maximum number of elements it can hold without ambiguity in front and rear pointers?
AN - 1
BN
CN + 1
DN / 2
Attempts:
2 left
💡 Hint
One slot is kept empty to distinguish full and empty states.

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 allows storing different data types in the same queue.
B. It efficiently uses the available space by reusing freed slots.
C. It automatically sorts the elements in the queue.
D. It increases the maximum size of the queue beyond the array length.

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 dequeued elements.
  3. Final Answer:

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

    Space reuse = Advantage [OK]
Hint: Circular queue reuses space freed by dequeues [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 way to check if a circular queue is full when using an array of size n with front and rear indices?
easy
A. if front == -1
B. if rear == front
C. if rear == n - 1
D. if (rear + 1) % n == front

Solution

  1. Step 1: Understand full condition in circular queue

    The queue is full when the next position of rear (circularly) equals front.
  2. Step 2: Apply modulo for circular increment

    Using modulo n ensures wrap-around, so (rear + 1) % n == front means full.
  3. Final Answer:

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

    Full condition uses modulo wrap [OK]
Hint: Full when next rear equals front modulo array size [OK]
Common Mistakes:
  • Checking rear == front means empty, not full
  • Using rear == n - 1 ignores circular wrap
  • Confusing front == -1 as full condition
3. Consider a circular queue of size 5 (indices 0 to 4). Initially, front = -1 and rear = -1. The following operations are performed:
enqueue(10), enqueue(20), enqueue(30), dequeue(), enqueue(40), enqueue(50), enqueue(60)
What will be the values of front and rear after these operations?
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 (removes 10)
    enqueue(40): rear=3
    enqueue(50): rear=4
    enqueue(60): rear=(4+1)%5=0 (wraps around)
  2. Step 2: Final front and rear values

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

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

    Wrap-around rear = 0, front moved after dequeue [OK]
Hint: Track front/rear changes step-by-step with modulo [OK]
Common Mistakes:
  • Not wrapping rear index after reaching end
  • Forgetting to update front after dequeue
  • Assuming front stays at -1 after first enqueue
4. Given the following Go code snippet for a circular queue, what is the bug that causes incorrect enqueue operation?
func enqueue(q []int, front *int, rear *int, n int, value int) {
    if (*rear+1)%n == *front {
        fmt.Println("Queue is full")
        return
    }
    if *front == -1 {
        *front = 0
    }
    *rear = (*rear + 1) % n
    q[*rear] = value
}
medium
A. The check for full queue is incorrect; it should be rear == front.
B. The queue slice q is passed by value, so changes to q are not saved.
C. The front pointer is not updated when queue is empty.
D. The rear index is incremented before checking if queue is full.

Solution

  1. Step 1: Check handling of empty queue

    The code updates front to 0 only if front == -1, handling the initial empty state correctly.
  2. Step 2: Issue after full dequeues

    After dequeuing all elements, dequeue updates front = (front + 1) % n, making (*rear + 1) % n == *front true, falsely detecting full. Enqueue fails to update/reset front for subsequent empty states.
  3. Final Answer:

    The front pointer is not updated when queue is empty. -> Option C
  4. Quick Check:

    Handle non-initial empty by resetting front [OK]
Hint: Reset front when queue empty after dequeues [OK]
Common Mistakes:
  • Confusing full condition with rear == front
  • Overlooking empty queue handling after dequeues
  • Thinking slice passing causes the issue
5. You have a circular queue implemented using an array of size 6. Initially, front = 4 and rear = 2. Which of the following sequences of operations will cause the queue to become full?
hard
A. dequeue, enqueue, enqueue, enqueue
B. enqueue, dequeue, enqueue, dequeue
C. dequeue, dequeue, enqueue, enqueue
D. dequeue, enqueue, dequeue

Solution

  1. Step 1: Understand initial state and full condition

    Array size n=6, front=4, rear=2 (5 elements occupied, 1 free). Full: (rear + 1) % 6 == front.
  2. Step 2: Simulate each option

    Only D causes final full state:
    dequeue: front=5, rear=2 (4 elem)
    enqueue: rear=3 (5 elem)
    enqueue: rear=4 (6 elem, now full since (4+1)%6=5==front)
    enqueue: full detected, no change. Final: front=5, rear=4 full.
    Others end not full.
  3. Final Answer:

    dequeue, enqueue, enqueue, enqueue -> Option A
  4. Quick Check:

    Simulate steps to check final full condition [OK]
Hint: Simulate front/rear moves to check full condition [OK]
Common Mistakes:
  • Assuming enqueue always fills queue immediately
  • Ignoring circular wrap in index calculations
  • Not updating front on dequeue
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