Queues are a linear data structure that store data in a “first in, first out” (FIFO) order. Unlike arrays, you cannot access elements by index and instead can only pull the next oldest element. This m
Implement a Queue class from scratch with an existing bug, the bug is that it cannot take more than 5 elements.
Implement a Queue using two stacks. You may only use the standard push()
, pop()
, and peek()
operations traditionally available to stacks. You do not need to implement the stack yourself (i.e. an array can be used to simulate a stack).
Queues are a linear data structure that store data in a “first in, first out” (FIFO) order. Unlike arrays, you cannot access elements by index and instead can only pull the next oldest element. This makes it great for order-sensitive tasks like online order processing or voicemail storage.
You can think of a queue as a line at the grocery store; the cashier does not choose who to check out next but rather processes the person who has stood in line the longest.
We could use a Python list with append()
and pop()
methods to implement a queue. However, this is inefficient because lists must shift all elements by one index whenever you add a new element to the beginning.
Instead, it’s best practice to use the deque
class from Python’s collections
module. Deques are optimized for the append and pop operations. The deque implementation also allows you to create double-ended queues, which can access both sides of the queue through the popleft()
and popright()
methods.
Advantages:
Automatically orders data chronologically
Scales to meet size requirements
Time efficient with deque
class
Disadvantages:
Can only access data on the ends
Applications:
Operations on a shared resource like a printer or CPU core
Serve as temporary storage for batch systems
Provides an easy default order for tasks of equal importance
Reverse first k elements of a queue
Implement a queue using a linked list
Implement a stack using a queue
Similar to a stack, the queue also limits the position for inserting and removing am operation on a sequence of data. However, unlike a stack, a queue follows the first in, first out (FIFO) principle.
In Python, a queue can also be implemented using a list. To follow the FIFO principle, the inserting operation occurs at the tail of the list, while the removing operation occurs at the head of the list.Python implementation of a queue
A Queue is a linear data structure in which data is stored in a First In, First Out manner. In a queue, the item that was added the earliest is removed first. The item that was added more recently is removed last. A queue can be compared to a real-life queue.
enqueue
is a queue operation where you add an item at the back of a queue.
dequeue
is a queue operation where you remove an item from the front of a queue.
Operating Systems - often maintain queues while implementing various low-level operations such as CPU Scheduling, Disk Scheduling, etc.
Hardware - hardware interrupts are handled using queues.
Internet - Website traffic handling.
And all other scenarios where a First In, First Out priority has to be implemented.
Queue Methods
queue.Enqueue()
The queue.Enqueue()
method adds an element at the rear of the queue.
Time Complexity -> O(1)
queue.Dequeue()
The queue.Dequeue()
method removes an element from the front of the queue.
Time Complexity -> O(1)
queue.Front()
The queue.Front()
method returns the front item from the queue.
Time Complexity -> O(1)
queue.Rear()
The queue.Rear()
method returns the rear item from the queue.
Time Complexity -> O(1)
queue.isEmpty()
The queue.isEmpty()
method returns True
if the queue is empty, else returns False
.
Time Complexity -> O(1)
Queues can be implemented in various ways. Let us look at how to implement a queue using a list and using the collections.deque
module in Python.
Queue using a List
We can use the list methods insert
and pop
to implement a queue.
Queue using collections.Deque
The deque
class from the python collections
module can also be used to implement a queue. This method of implementing a queue is far more efficient because deque provides faster enqueue and dequeue operations.
Try implementing the queue in Python first. Then once you’re done with the implementation, try solving these problems on HackerRank and LeetCode
Reversing a Queue - GeeksforGeeks
Sort the Queue using Recursion - GeeksforGeeks
Reversing First K Elements of the Queue - GeeksforGeeks
Implementing Stack using Queues - GeeksforGeeks
Queries with Fixed Length - HackerRank
Truck Tour - HackerRank
Maximum Sum of Triangle No Less Than K - LeetCode
Design Circular Queue - LeetCode
Design Circular Dequeue - LeetCode