Implementing a Queue in Python
Queues are fundamental data structures used in many algorithms and applications, such as task scheduling, breadth-first search, and managing requests. This challenge focuses on understanding and implementing queue operations in Python. You'll gain hands-on experience with a core programming concept by creating a functional queue.
Problem Description
Your task is to implement a queue data structure in Python. A queue follows the First-In, First-Out (FIFO) principle, meaning the first element added to the queue is the first one to be removed. You need to implement the following essential queue operations:
enqueue(item): Adds anitemto the rear (end) of the queue.dequeue(): Removes and returns the item from the front of the queue. If the queue is empty, it should raise an appropriate error (e.g.,IndexError).is_empty(): ReturnsTrueif the queue contains no items, andFalseotherwise.peek(): Returns the item at the front of the queue without removing it. If the queue is empty, it should raise an appropriate error (e.g.,IndexError).size(): Returns the number of items currently in the queue.
You should encapsulate these operations within a class, let's call it Queue.
Examples
Example 1:
# Initialize a queue
my_queue = Queue()
# Enqueue elements
my_queue.enqueue(10)
my_queue.enqueue(20)
my_queue.enqueue(30)
# Check size and peek
print(my_queue.size()) # Output: 3
print(my_queue.peek()) # Output: 10
# Dequeue elements
print(my_queue.dequeue()) # Output: 10
print(my_queue.dequeue()) # Output: 20
# Check if empty
print(my_queue.is_empty()) # Output: False
print(my_queue.size()) # Output: 1
print(my_queue.peek()) # Output: 30
print(my_queue.dequeue()) # Output: 30
print(my_queue.is_empty()) # Output: True
Example 2:
# Initialize an empty queue
empty_queue = Queue()
# Check if empty
print(empty_queue.is_empty()) # Output: True
print(empty_queue.size()) # Output: 0
# Attempt to dequeue from an empty queue (should raise an error)
try:
empty_queue.dequeue()
except IndexError as e:
print(e) # Output: dequeue from empty queue
# Attempt to peek at an empty queue (should raise an error)
try:
empty_queue.peek()
except IndexError as e:
print(e) # Output: peek from empty queue
Constraints
- The
Queueclass should be implemented using standard Python data structures (e.g., lists orcollections.deque). - The
enqueueanddequeueoperations should ideally have an average time complexity of O(1) if possible, depending on the underlying data structure used. - The
is_empty,peek, andsizeoperations should have a time complexity of O(1). - The data types of items enqueued can be any Python object.
- Error handling for operations on an empty queue must be implemented as specified.
Notes
- Consider using Python's built-in
collections.dequefor an efficient queue implementation, as it offers O(1) append and pop operations from both ends. Alternatively, a Python list can be used, but be mindful of potential performance implications forpop(0). - When raising errors, provide informative messages to help users understand what went wrong. For instance, "dequeue from empty queue" or "peek from empty queue."
- The goal is to create a robust and efficient
Queueclass that correctly models the FIFO behavior.