Challenge - 5 Problems
Queue Peek Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of peeking the front element in this queue?
Consider a queue implemented using an array with front at index 0 and rear at index 3. The queue elements are [10, 20, 30, 40]. What will be the output of the peek operation?
DSA C
int queue[] = {10, 20, 30, 40}; int front = 0; int rear = 3; int peek = queue[front]; printf("%d\n", peek);
Attempts:
2 left
💡 Hint
The front element is the first element inserted in the queue.
✗ Incorrect
In a queue, the front points to the first element inserted. Here, front is 0, so queue[front] is 10.
❓ Predict Output
intermediate2:00remaining
What happens when peeking an empty queue?
Given a queue with front = -1 and rear = -1 indicating it is empty, what will happen if we try to peek the front element?
DSA C
int front = -1; int rear = -1; if(front == -1) { printf("Queue is empty\n"); } else { printf("%d\n", queue[front]); }
Attempts:
2 left
💡 Hint
Check the condition that indicates the queue is empty before peeking.
✗ Incorrect
When front is -1, the queue is empty, so peeking should not access any element and print 'Queue is empty'.
🔧 Debug
advanced2:00remaining
Why does this peek function cause an error?
This peek function is intended to return the front element of a queue. Identify the error and its cause.
DSA C
int peek(int queue[], int front, int rear) { if(front > rear) { printf("Queue is empty\n"); return -1; } return queue[rear]; }
Attempts:
2 left
💡 Hint
Peek should return the element at the front index, not rear.
✗ Incorrect
The function returns queue[rear], which is the last element, not the front. It should return queue[front].
🧠 Conceptual
advanced1:00remaining
What is the time complexity of peeking the front element in a queue?
Assuming a queue is implemented using a linked list or array, what is the time complexity of the peek operation?
Attempts:
2 left
💡 Hint
Peek just returns the front element without traversal.
✗ Incorrect
Peek accesses the front element directly, so it takes constant time O(1).
🚀 Application
expert3:00remaining
After these operations, what is the front element of the queue?
A queue is initially empty. We perform enqueue(5), enqueue(10), dequeue(), enqueue(15), dequeue(), enqueue(20). What is the front element now?
DSA C
Operations: 1. enqueue(5) 2. enqueue(10) 3. dequeue() 4. enqueue(15) 5. dequeue() 6. enqueue(20) Assume standard FIFO queue behavior.
Attempts:
2 left
💡 Hint
Track the queue state after each operation carefully.
✗ Incorrect
After enqueue(5) and enqueue(10), queue is [5,10]. dequeue removes 5, queue is [10]. enqueue(15) adds 15, queue is [10,15]. dequeue removes 10, queue is [15]. enqueue(20) adds 20, queue is [15,20]. Front is 15.
