easy +10 pts

Queue class (list-based)

Implement a FIFO queue using a Python list with a fixed capacity.

Implement a class named `Queue` that models a first-in, first-out (FIFO) queue using a Python list as the underlying storage. The class must support a fixed capacity. Define the constructor `__init__(self, capacity)` that initializes an empty queue with the given maximum number of items. Assume `capacity` is a positive integer. Implement the following methods: - `enqueue(self, item)` – Adds `item` to the back of the queue. If the queue is already full, raise an `IndexError` with the message `"Queue is full"`. - `dequeue(self)` – Removes and returns the front item. If the queue is empty, raise an `IndexError` with the message `"Queue is empty"`. - `peek(self)` – Returns the front item without removing it. If the queue is empty, raise an `IndexError` with the message `"Queue is empty"`. - `is_empty(self)` – Returns `True` if the queue has no items, otherwise `False`. - `is_full(self)` – Returns `True` if the queue has reached its capacity, otherwise `False`. All items are stored in order, and the queue must behave as a standard FIFO structure.

Constraints

- `capacity` is a positive integer. - The queue can hold at most `capacity` items. - Items can be of any type. - The queue must maintain FIFO order. - All operations must work in O(1) time except `dequeue` which may be O(n) due to list popping from front.

Example

>>> q = Queue(2)
>>> q.is_empty()
True
>>> q.enqueue(10)
>>> q.enqueue(20)
>>> q.is_full()
True
>>> q.peek()
10
>>> q.dequeue()
10
>>> q.dequeue()
20
>>> q.is_empty()
True
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `self._items` as a list to store elements and `self._capacity` to track max size.
For `enqueue`, check `is_full` first; use `self._items.append(item)`.
For `dequeue`, check `is_empty` first; use `self._items.pop(0)` to remove the front.
For `peek`, return `self._items[0]` after checking it is not empty.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.