Challenge - 5 Problems
Queue Peek Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of peeking the front element in this queue?
Consider the following JavaScript code that implements a queue using an array. What will be printed after peeking the front element?
DSA Javascript
class Queue { constructor() { this.items = []; } enqueue(element) { this.items.push(element); } dequeue() { if(this.isEmpty()) return null; return this.items.shift(); } peek() { if(this.isEmpty()) return null; return this.items[0]; } isEmpty() { return this.items.length === 0; } } const q = new Queue(); q.enqueue(10); q.enqueue(20); q.enqueue(30); console.log(q.peek());
Attempts:
2 left
💡 Hint
Peek returns the first element without removing it.
✗ Incorrect
The peek method returns the first element in the queue without removing it. Since 10 was enqueued first, it is at the front.
❓ Predict Output
intermediateWhat does this peek operation return after dequeue?
Given the queue implementation below, what will be the output of the peek method after one dequeue operation?
DSA Javascript
class Queue { constructor() { this.items = []; } enqueue(element) { this.items.push(element); } dequeue() { if(this.isEmpty()) return null; return this.items.shift(); } peek() { if(this.isEmpty()) return null; return this.items[0]; } isEmpty() { return this.items.length === 0; } } const q = new Queue(); q.enqueue(5); q.enqueue(15); q.enqueue(25); q.dequeue(); console.log(q.peek());
Attempts:
2 left
💡 Hint
Dequeue removes the first element, so peek shows the next.
✗ Incorrect
After dequeue removes 5, the front element becomes 15, so peek returns 15.
🧠 Conceptual
advancedWhat is the time complexity of the peek operation in a queue implemented with an array?
Assuming a queue is implemented using a JavaScript array with enqueue adding to the end and dequeue removing from the front, what is the time complexity of the peek operation?
Attempts:
2 left
💡 Hint
Peek just accesses the first element without removing it.
✗ Incorrect
Peek accesses the first element directly by index, which is a constant time operation O(1).
❓ Predict Output
advancedWhat error does this peek method cause?
What error will be thrown when calling peek on an empty queue in this code?
DSA Javascript
class Queue { constructor() { this.items = []; } enqueue(element) { this.items.push(element); } dequeue() { return this.items.shift(); } peek() { return this.items[0].value; } } const q = new Queue(); console.log(q.peek());
Attempts:
2 left
💡 Hint
Accessing property 'value' of undefined causes a TypeError.
✗ Incorrect
Since the queue is empty, items[0] is undefined. Trying to access .value on undefined throws a TypeError.
🚀 Application
expertAfter these operations, what is the front element of the queue?
Given the following sequence of operations on a queue, what will peek return?
DSA Javascript
class Queue { constructor() { this.items = []; } enqueue(element) { this.items.push(element); } dequeue() { if(this.isEmpty()) return null; return this.items.shift(); } peek() { if(this.isEmpty()) return null; return this.items[0]; } isEmpty() { return this.items.length === 0; } } const q = new Queue(); q.enqueue('a'); q.enqueue('b'); q.enqueue('c'); q.dequeue(); q.enqueue('d'); q.dequeue(); console.log(q.peek());
Attempts:
2 left
💡 Hint
Track the queue after each operation carefully.
✗ Incorrect
Operations:
- enqueue 'a', 'b', 'c' → queue: ['a', 'b', 'c']
- dequeue removes 'a' → queue: ['b', 'c']
- enqueue 'd' → queue: ['b', 'c', 'd']
- dequeue removes 'b' → queue: ['c', 'd']
Peek returns 'c', the front element.
