Challenge - 5 Problems
Queue Mastery Badge
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 queue implemented using an array with a fixed size of 5. Initially, the queue is empty. We perform the following operations in order:
enqueue(10), enqueue(20), enqueue(30), dequeue(), enqueue(40), dequeue(), enqueue(50).
What is the state of the queue after these operations?
enqueue(10), enqueue(20), enqueue(30), dequeue(), enqueue(40), dequeue(), enqueue(50).
What is the state of the queue after these operations?
DSA Typescript
class Queue { private items: number[] = []; private maxSize: number; constructor(size: number) { this.maxSize = size; } enqueue(element: number): boolean { if (this.items.length === this.maxSize) return false; this.items.push(element); return true; } dequeue(): number | null { if (this.items.length === 0) return null; return this.items.shift()!; } getQueue(): number[] { return this.items; } } const q = new Queue(5); q.enqueue(10); q.enqueue(20); q.enqueue(30); q.dequeue(); q.enqueue(40); q.dequeue(); q.enqueue(50); console.log(q.getQueue());
Attempts:
2 left
💡 Hint
Remember that dequeue removes the front element and enqueue adds to the back.
✗ Incorrect
After enqueue(10), enqueue(20), enqueue(30), the queue is [10, 20, 30]. dequeue() removes 10, queue becomes [20, 30]. enqueue(40) adds 40: [20, 30, 40]. dequeue() removes 20: [30, 40]. enqueue(50) adds 50: [30, 40, 50].
🧠 Conceptual
intermediateWhat happens when enqueue is called on a full queue?
In a queue implemented using a fixed-size array, what is the expected behavior when you try to enqueue an element but the queue is already full?
Attempts:
2 left
💡 Hint
Think about fixed size and how arrays behave when full.
✗ Incorrect
In a fixed-size array queue, when full, enqueue cannot add new elements. It should fail gracefully, often returning false or throwing an error.
🔧 Debug
advancedIdentify the bug in this dequeue method
The following dequeue method is part of a queue implemented using an array. It is supposed to remove and return the front element. What is the bug in this code?
DSA Typescript
dequeue(): number | null {
if (this.items.length === 0) return null;
return this.items.pop();
}Attempts:
2 left
💡 Hint
Remember that queues remove from the front, not the back.
✗ Incorrect
pop() removes the last element of the array, but dequeue should remove the first element. The correct method is shift().
📝 Syntax
advancedWhich option correctly implements enqueue with capacity check?
Choose the correct enqueue method implementation that adds an element to the queue only if there is space left in the fixed-size array.
Attempts:
2 left
💡 Hint
Check the condition and method used to add element at the end.
✗ Incorrect
Option D correctly checks if length is >= maxSize to prevent overflow, then pushes element at the end and returns true. Others either have wrong condition or use unshift which adds at front.
🚀 Application
expertHow many items remain after circular queue operations?
A circular queue is implemented using an array of size 4. Initially empty, the following operations are performed:
enqueue(1), enqueue(2), enqueue(3), dequeue(), enqueue(4), enqueue(5), dequeue(), enqueue(6).
How many items are currently in the queue?
enqueue(1), enqueue(2), enqueue(3), dequeue(), enqueue(4), enqueue(5), dequeue(), enqueue(6).
How many items are currently in the queue?
Attempts:
2 left
💡 Hint
Remember circular queue wraps around and max size is 4.
✗ Incorrect
Operations:
enqueue(1) -> size 1
enqueue(2) -> size 2
enqueue(3) -> size 3
dequeue() removes 1 -> size 2
enqueue(4) -> size 3
enqueue(5) -> size 4 (full)
dequeue() removes 2 -> size 3
enqueue(6) -> size 4 (full)
So final size is 4, but since max size is 4, after last enqueue size is 4.
But the question asks how many items currently in the queue after all operations, which is 4.
enqueue(1) -> size 1
enqueue(2) -> size 2
enqueue(3) -> size 3
dequeue() removes 1 -> size 2
enqueue(4) -> size 3
enqueue(5) -> size 4 (full)
dequeue() removes 2 -> size 3
enqueue(6) -> size 4 (full)
So final size is 4, but since max size is 4, after last enqueue size is 4.
But the question asks how many items currently in the queue after all operations, which is 4.
