Challenge - 5 Problems
Circular Queue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after enqueue and dequeue operations?
Consider a circular queue of size 5 implemented using an array. Initially, the queue is empty. The following operations are performed:
enqueue(10), enqueue(20), enqueue(30), dequeue(), enqueue(40), enqueue(50), enqueue(60), dequeue(), enqueue(70)
What is the state of the queue from front to rear after these operations?
enqueue(10), enqueue(20), enqueue(30), dequeue(), enqueue(40), enqueue(50), enqueue(60), dequeue(), enqueue(70)
What is the state of the queue from front to rear after these operations?
DSA Javascript
class CircularQueue { constructor(size) { this.size = size; this.queue = new Array(size); this.front = -1; this.rear = -1; } enqueue(value) { if ((this.rear + 1) % this.size === this.front) { return 'Queue is full'; } if (this.front === -1) { this.front = 0; } this.rear = (this.rear + 1) % this.size; this.queue[this.rear] = value; } dequeue() { if (this.front === -1) { return 'Queue is empty'; } const value = this.queue[this.front]; if (this.front === this.rear) { this.front = -1; this.rear = -1; } else { this.front = (this.front + 1) % this.size; } return value; } printQueue() { if (this.front === -1) { return 'Queue is empty'; } let result = []; let i = this.front; while (true) { result.push(this.queue[i]); if (i === this.rear) break; i = (i + 1) % this.size; } return result.join(' -> ') + ' -> null'; } } const cq = new CircularQueue(5); cq.enqueue(10); cq.enqueue(20); cq.enqueue(30); cq.dequeue(); cq.enqueue(40); cq.enqueue(50); cq.enqueue(60); cq.dequeue(); cq.enqueue(70); console.log(cq.printQueue());
Attempts:
2 left
💡 Hint
Remember that the circular queue wraps around when rear reaches the end of the array.
✗ Incorrect
After the operations, the queue contains elements 30, 40, 50, 60, and 70 in order from front to rear. The dequeue operations remove 10 and then 20, so they are no longer in the queue.
❓ Predict Output
intermediateWhat error occurs when enqueueing into a full circular queue?
Given a circular queue of size 3, the following operations are performed:
enqueue(1), enqueue(2), enqueue(3), enqueue(4)
What is the output or error after the last enqueue operation?
enqueue(1), enqueue(2), enqueue(3), enqueue(4)
What is the output or error after the last enqueue operation?
DSA Javascript
class CircularQueue { constructor(size) { this.size = size; this.queue = new Array(size); this.front = -1; this.rear = -1; } enqueue(value) { if ((this.rear + 1) % this.size === this.front) { return 'Queue is full'; } if (this.front === -1) { this.front = 0; } this.rear = (this.rear + 1) % this.size; this.queue[this.rear] = value; return 'Enqueued ' + value; } } const cq = new CircularQueue(3); console.log(cq.enqueue(1)); console.log(cq.enqueue(2)); console.log(cq.enqueue(3)); console.log(cq.enqueue(4));
Attempts:
2 left
💡 Hint
Check the condition that detects if the queue is full before adding a new element.
✗ Incorrect
The circular queue is full when the next position of rear equals front. Trying to enqueue when full returns 'Queue is full'.
🔧 Debug
advancedIdentify the bug in this circular queue dequeue method
The following dequeue method is part of a circular queue implementation. It sometimes causes an infinite loop or incorrect front pointer update. What is the bug?
```js dequeue() { if (this.front === -1) { return 'Queue is empty'; } const value = this.queue[this.front]; if (this.front === this.rear) { this.front = -1; this.rear = -1; } else { this.front = this.front + 1 % this.size; } return value; } ```
```js dequeue() { if (this.front === -1) { return 'Queue is empty'; } const value = this.queue[this.front]; if (this.front === this.rear) { this.front = -1; this.rear = -1; } else { this.front = this.front + 1 % this.size; } return value; } ```
Attempts:
2 left
💡 Hint
Operator precedence matters in arithmetic expressions.
✗ Incorrect
Without parentheses, this.front + 1 % this.size is evaluated as this.front + (1 % this.size), which is incorrect. It should be (this.front + 1) % this.size to wrap correctly.
🚀 Application
advancedHow many elements can a circular queue of size N hold?
A circular queue is implemented using an array of size N. To distinguish between full and empty states, one slot is kept empty. What is the maximum number of elements that can be stored in this circular queue?
Attempts:
2 left
💡 Hint
One slot is sacrificed to differentiate full and empty states.
✗ Incorrect
To avoid confusion between full and empty, circular queue implementations keep one slot empty, so maximum elements stored is N - 1.
🧠 Conceptual
expertWhy is a circular queue preferred over a linear queue in fixed-size array implementations?
Choose the best reason why circular queues are preferred over linear queues when implemented using fixed-size arrays.
Attempts:
2 left
💡 Hint
Think about what happens when elements are dequeued in a linear queue.
✗ Incorrect
In a linear queue, once rear reaches the end, no more elements can be enqueued even if there is space at the front. Circular queues wrap around and reuse that space.
