Challenge - 5 Problems
Circular Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Circular Queue Enqueue and Dequeue Operations
What is the printed state of the circular queue after the following operations?
DSA Typescript
class CircularQueue { private queue: number[]; private front: number; private rear: number; private size: number; private capacity: number; constructor(capacity: number) { this.capacity = capacity; this.queue = new Array(capacity); this.front = -1; this.rear = -1; this.size = 0; } enqueue(value: number): boolean { if (this.size === this.capacity) return false; if (this.size === 0) this.front = 0; this.rear = (this.rear + 1) % this.capacity; this.queue[this.rear] = value; this.size++; return true; } dequeue(): number | null { if (this.size === 0) return null; const value = this.queue[this.front]; if (this.size === 1) { this.front = -1; this.rear = -1; } else { this.front = (this.front + 1) % this.capacity; } this.size--; return value; } printQueue(): string { if (this.size === 0) return "empty"; let result = ""; for (let i = 0; i < this.size; i++) { const index = (this.front + i) % this.capacity; result += this.queue[index] + (i < this.size - 1 ? " -> " : " -> null"); } return result; } } 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
Track front and rear indices carefully after each enqueue and dequeue.
✗ Incorrect
After the operations, the queue contains elements 30, 40, 50, 60, 70 in order, starting from front index. The printQueue method shows them linked with arrows ending with null.
🧠 Conceptual
intermediateCapacity Behavior in Circular Queue
If a circular queue has capacity 4 and currently holds 4 elements, what happens when you try to enqueue one more element?
Attempts:
2 left
💡 Hint
Think about fixed size and how circular queue manages full state.
✗ Incorrect
A circular queue with fixed capacity cannot add more elements when full. Enqueue returns false or fails.
🔧 Debug
advancedIdentify the Bug in Circular Queue Dequeue Method
What error will occur when running this dequeue method if the queue is empty?
DSA Typescript
dequeue(): number {
const value = this.queue[this.front];
if (this.size === 1) {
this.front = -1;
this.rear = -1;
} else {
this.front = (this.front + 1) % this.capacity;
}
this.size--;
return value;
}Attempts:
2 left
💡 Hint
Check what happens if dequeue is called on an empty queue.
✗ Incorrect
If the queue is empty, front is -1, so accessing this.queue[-1] returns undefined, but returning undefined as number causes TypeError in strict typing or unexpected behavior.
📝 Syntax
advancedIdentify Syntax Error in Circular Queue Enqueue Method
Which option contains a syntax error in the enqueue method?
DSA Typescript
enqueue(value: number): boolean {
if (this.size === this.capacity) return false;
if (this.size === 0) this.front = 0;
this.rear = (this.rear + 1) % this.capacity;
this.queue[this.rear] = value;
this.size++;
return true;
}Attempts:
2 left
💡 Hint
Check missing punctuation in TypeScript statements.
✗ Incorrect
In TypeScript, missing semicolon after return statement can cause syntax error in some contexts.
🚀 Application
expertDetermine Final Queue State After Complex Operations
Given a circular queue of capacity 6, after these operations, what is the printed queue state?
Operations:
- enqueue 5
- enqueue 10
- enqueue 15
- dequeue
- enqueue 20
- enqueue 25
- dequeue
- enqueue 30
- enqueue 35
- dequeue
- enqueue 40
- enqueue 45
- dequeue
- enqueue 50
Print the queue using the format: value -> value -> ... -> null
DSA Typescript
class CircularQueue { private queue: number[]; private front: number; private rear: number; private size: number; private capacity: number; constructor(capacity: number) { this.capacity = capacity; this.queue = new Array(capacity); this.front = -1; this.rear = -1; this.size = 0; } enqueue(value: number): boolean { if (this.size === this.capacity) return false; if (this.size === 0) this.front = 0; this.rear = (this.rear + 1) % this.capacity; this.queue[this.rear] = value; this.size++; return true; } dequeue(): number | null { if (this.size === 0) return null; const value = this.queue[this.front]; if (this.size === 1) { this.front = -1; this.rear = -1; } else { this.front = (this.front + 1) % this.capacity; } this.size--; return value; } printQueue(): string { if (this.size === 0) return "empty"; let result = ""; for (let i = 0; i < this.size; i++) { const index = (this.front + i) % this.capacity; result += this.queue[index] + (i < this.size - 1 ? " -> " : " -> null"); } return result; } } const cq = new CircularQueue(6); cq.enqueue(5); cq.enqueue(10); cq.enqueue(15); cq.dequeue(); cq.enqueue(20); cq.enqueue(25); cq.dequeue(); cq.enqueue(30); cq.enqueue(35); cq.dequeue(); cq.enqueue(40); cq.enqueue(45); cq.dequeue(); cq.enqueue(50); console.log(cq.printQueue());
Attempts:
2 left
💡 Hint
Track front and rear indices carefully after each enqueue and dequeue operation.
✗ Incorrect
After all operations, the queue contains 30, 35, 40, 45, 50 in order starting from front index. The printQueue method shows them linked with arrows ending with null.
