📖 Scenario: You are managing a line of customers waiting to buy tickets at a counter. The line is a queue where the first person to arrive is the first to be served.
🎯 Goal: Build a simple queue using a list and learn how to peek at the front customer without removing them from the line.
📋 What You'll Learn
Create a queue with exact customers in order
Add a variable to hold the front element
Access the front element without removing it
Print the front element
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting for service or tasks waiting to be processed.
💼 Career
Understanding queues and how to peek at elements is important for software development, especially in areas like task scheduling, messaging systems, and resource management.
Progress0 / 4 steps
1
Create the queue with customers
Create a list called queue with these exact customers in order: 'Alice', 'Bob', 'Charlie', 'Diana'
DSA Python
Hint
Use square brackets to create a list and separate names with commas.
2
Add a variable to hold the front element
Create a variable called front_customer and set it to the first element of the queue list using index 0
DSA Python
Hint
Use queue[0] to get the first item in the list.
3
Access the front element without removing it
Use the variable front_customer to hold the front element of the queue without removing it. (This is already done in the previous step.)
DSA Python
Hint
You already have the front element stored in front_customer.
4
Print the front element
Print the value of the variable front_customer to show the front element of the queue
DSA Python
Hint
Use print(front_customer) to display the front customer.
Practice
(1/5)
1. What does the peek operation do in a queue?
easy
A. Returns the front element without removing it
B. Removes the front element from the queue
C. Adds an element to the back of the queue
D. Checks if the queue is empty
Solution
Step 1: Understand the peek operation
The peek operation looks at the front element of the queue without changing the queue.
Step 2: Compare with other operations
Unlike dequeue, peek does not remove the element; it only returns it.
Final Answer:
Returns the front element without removing it -> Option A
Quick Check:
Peek = front element without removal [OK]
Hint: Peek shows front without removing it [OK]
Common Mistakes:
Confusing peek with dequeue
Thinking peek adds elements
Assuming peek checks emptiness
2. Which of the following is the correct syntax to peek the front element of a queue named q in Python using collections.deque?
easy
A. front = q[0]
B. front = q.peek()
C. front = q.front()
D. front = q.get()
Solution
Step 1: Recall deque indexing
In Python's deque, the front element is accessed by index 0, like q[0].
Step 2: Check other options
Methods peek(), front(), and get() do not exist for deque.
After q.popleft(), q becomes [20, 30], so q[0] is now 20.
Final Answer:
10\n20 -> Option D
Quick Check:
First peek 10, after removal peek 20 [OK]
Hint: Peek before and after popleft to see front change [OK]
Common Mistakes:
Assuming front stays same after popleft
Confusing popleft with pop
Printing wrong elements
4. Identify the error in the following code that tries to peek the front element of a queue:
from queue import Queue
q = Queue()
q.put(5)
q.put(10)
print(q[0])
medium
A. print statement syntax error
B. Queue is empty, cannot peek
C. Queue object does not support indexing
D. put() method is incorrect
Solution
Step 1: Understand Queue class behavior
Python's queue.Queue does not support direct indexing like q[0].
Step 2: Identify correct peek method
To peek, one must use q.queue[0] or other methods, but direct q[0] causes error.
Final Answer:
Queue object does not support indexing -> Option C
Quick Check:
queue.Queue no indexing [OK]
Hint: queue.Queue objects can't use q[0] indexing [OK]
Common Mistakes:
Trying to index queue.Queue directly
Assuming put() is wrong
Thinking queue is empty
5. You have a queue implemented using a list in Python: q = [5, 10, 15]. You want to peek the front element safely without causing an error if the queue is empty. Which code snippet correctly does this?
hard
A. front = q.pop(0)
B. front = q[0] if len(q) > 0 else None
C. front = q[-1]
D. front = q.get()
Solution
Step 1: Check for empty queue before peeking
Accessing q[0] when q is empty causes error, so check length first.
Step 2: Understand other options
pop(0) removes element, not peek; q[-1] accesses last element; q.get() is invalid for list.
Final Answer:
front = q[0] if len(q) > 0 else None -> Option B
Quick Check:
Safe peek with length check [OK]
Hint: Check length before accessing q[0] to avoid errors [OK]
Common Mistakes:
Using pop(0) which removes element
Accessing q[0] without checking empty
Using q.get() on list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.