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 after peeking the front element?
Consider a queue implemented using an array. What will be the output of peeking the front element after the given operations?
DSA Typescript
class Queue { private items: number[] = []; enqueue(element: number) { this.items.push(element); } dequeue() { return this.items.shift(); } peek() { return this.items[0]; } } const queue = new Queue(); queue.enqueue(10); queue.enqueue(20); queue.enqueue(30); queue.dequeue(); console.log(queue.peek());
Attempts:
2 left
💡 Hint
Remember that dequeue removes the front element before peeking.
✗ Incorrect
Initially, the queue has [10, 20, 30]. After dequeue, 10 is removed, so the front is 20. Peeking returns 20.
❓ Predict Output
intermediateWhat does peek return on an empty queue?
What will be the output of peeking the front element of an empty queue implemented with an array?
DSA Typescript
class Queue { private items: number[] = []; peek() { return this.items[0]; } } const queue = new Queue(); console.log(queue.peek());
Attempts:
2 left
💡 Hint
Accessing the first element of an empty array returns what?
✗ Incorrect
Accessing index 0 of an empty array returns undefined in JavaScript/TypeScript.
❓ Predict Output
advancedWhat is the output after multiple enqueues and peeks?
Given the following queue operations, what is printed after the last peek?
DSA Typescript
class Queue { private items: number[] = []; enqueue(element: number) { this.items.push(element); } peek() { return this.items[0]; } } const queue = new Queue(); queue.enqueue(5); queue.enqueue(15); queue.enqueue(25); console.log(queue.peek()); queue.enqueue(35); console.log(queue.peek());
Attempts:
2 left
💡 Hint
Peek always returns the first element without removing it.
✗ Incorrect
Peek returns the first element which is 5 both times since no dequeue happened.
❓ Predict Output
advancedWhat error occurs when peeking after clearing the queue?
What will be the output or error after peeking the front element of a queue cleared by removing all elements?
DSA Typescript
class Queue { private items: number[] = []; enqueue(element: number) { this.items.push(element); } dequeue() { return this.items.shift(); } peek() { return this.items[0]; } } const queue = new Queue(); queue.enqueue(1); queue.enqueue(2); queue.dequeue(); queue.dequeue(); console.log(queue.peek());
Attempts:
2 left
💡 Hint
What happens when you access the first element of an empty array?
✗ Incorrect
After removing all elements, the array is empty and accessing index 0 returns undefined.
🧠 Conceptual
expertWhy is peeking the front element of a queue O(1) time complexity?
Choose the best explanation why peeking the front element of a queue is a constant time operation.
Attempts:
2 left
💡 Hint
Think about how queues store their front element.
✗ Incorrect
Queues keep track of the front element directly, so accessing it does not require looping.
