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 C# code that uses a queue. What will be printed after peeking the front element?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { Queue<int> q = new Queue<int>(); q.Enqueue(10); q.Enqueue(20); q.Enqueue(30); Console.WriteLine(q.Peek()); } }
Attempts:
2 left
💡 Hint
Peek returns the front element without removing it.
✗ Incorrect
The queue has elements 10, 20, 30 in that order. Peek returns the first element, which is 10.
❓ Predict Output
intermediateWhat happens when peeking an empty queue?
What will happen if you call Peek on an empty queue in C#?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { Queue<int> q = new Queue<int>(); Console.WriteLine(q.Peek()); } }
Attempts:
2 left
💡 Hint
Peek on empty queue throws an exception.
✗ Incorrect
Calling Peek on an empty queue throws InvalidOperationException because there is no element to return.
🔧 Debug
advancedWhy does this code throw an exception when peeking?
This code throws an exception at runtime. What is the cause?
DSA C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<string> q = new Queue<string>();
q.Enqueue(null);
Console.WriteLine(q.Peek().Length);
}
}Attempts:
2 left
💡 Hint
Peek returns the front element, which is null here.
✗ Incorrect
The queue contains one element which is null. Peek returns null, so accessing Length causes NullReferenceException.
❓ Predict Output
advancedWhat is the queue state after peeking?
What is the state of the queue after peeking the front element?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { Queue<int> q = new Queue<int>(); q.Enqueue(5); q.Enqueue(15); q.Enqueue(25); int front = q.Peek(); Console.WriteLine(front); Console.WriteLine(string.Join(",", q)); } }
Attempts:
2 left
💡 Hint
Peek does not remove the element.
✗ Incorrect
Peek returns the front element without removing it, so queue remains unchanged.
🧠 Conceptual
expertWhich statement about Peek in a queue is true?
Select the correct statement about the Peek operation on a queue.
Attempts:
2 left
💡 Hint
Think about what Peek does and what happens if the queue is empty.
✗ Incorrect
Peek returns the front element without removing it. If the queue is empty, it throws InvalidOperationException.
