Junior — Middle
Can you explain what a queue data structure is?
sobes.tech AI
Answer from AI
A queue is a data structure that operates on the FIFO (First In, First Out) principle, meaning the first element added will be the first to be processed.
Main operations of a queue:
- enqueue (adding an element to the end of the queue)
- dequeue (removing and retrieving an element from the front of the queue)
Example of using a queue in Python with the collections module:
from collections import deque
queue = deque()
# Adding elements
queue.append('task1')
queue.append('task2')
# Removing elements
first_task = queue.popleft() # 'task1'
second_task = queue.popleft() # 'task2'
Queues are widely used for organizing sequential task processing, for example, in message processing systems, task schedulers, and multithreading.