Hone logo
Hone
Problems

Implementing a Queue Data Structure in Python

Queues are fundamental data structures that follow the First-In, First-Out (FIFO) principle. This challenge asks you to implement a queue using Python's built-in data structures (specifically, a list) and define the core queue operations: enqueue, dequeue, peek, is_empty, and size. Understanding and implementing queues is crucial for various applications like task scheduling, breadth-first search, and managing requests.

Problem Description

You are tasked with creating a Queue class in Python. This class should provide the following functionalities:

  • enqueue(item): Adds an item to the rear of the queue.
  • dequeue(): Removes and returns the item at the front of the queue. If the queue is empty, it should return None.
  • peek(): Returns the item at the front of the queue without removing it. If the queue is empty, it should return None.
  • is_empty(): Returns True if the queue is empty, False otherwise.
  • size(): Returns the number of items currently in the queue.

The queue should be implemented using a Python list. Ensure your implementation adheres to the FIFO principle. Consider edge cases such as an empty queue when attempting to dequeue or peek.

Examples

Example 1:

Input:
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
print(q.dequeue())
print(q.peek())
print(q.size())

Output:

1
2
2

Explanation: We enqueue 1, 2, and 3. Dequeue removes and returns 1. Peek returns 2 (without removing it). The size is now 2.

Example 2:

Input:
q = Queue()
print(q.dequeue())
print(q.peek())
print(q.is_empty())

Output:

None
None
True

Explanation: The queue is initially empty. Dequeue returns None. Peek returns None. is_empty returns True.

Example 3: (Edge Case)

Input:
q = Queue()
q.enqueue("a")
q.enqueue("b")
q.dequeue()
q.dequeue()
print(q.is_empty())

Output:

True

Explanation: We enqueue "a" and "b". We dequeue both, leaving the queue empty. is_empty returns True.

Constraints

  • The queue should be implemented using a Python list.
  • The enqueue and dequeue operations should have an average time complexity of O(1). While list operations can be O(n) in the worst case, aim for an implementation that minimizes this.
  • The peek, is_empty, and size operations should have a time complexity of O(1).
  • The queue can hold any type of Python object.
  • The queue will not be subjected to extremely large numbers of operations during testing (less than 1000 enqueues/dequeues).

Notes

  • Think carefully about how to use the Python list to efficiently implement the FIFO behavior.
  • Consider how to handle the edge case of an empty queue for dequeue and peek operations.
  • While using collections.deque would be a more efficient solution in a production environment, this challenge specifically asks you to implement the queue using a Python list. Focus on understanding the underlying principles of a queue rather than optimizing for performance.
Loading editor...
python