What is a queue?
A queue is a data structure that works on the FIFO (First In, First Out) principle: first in, first out.
That means the element added first is removed before the others.
Main queue operations:
- enqueue(x) - add element
xto the end of the queue, - dequeue() - remove and return the element from the front of the queue,
- peek() / front() - look at the element at the front without removing it,
- isEmpty() - check whether the queue is empty.
Example: a line of people in a store - whoever got there first is served first.
A queue is often used:
- for task scheduling (processes, threads, printers),
- in networking (packet processing),
- for breadth-first graph traversal (BFS),
- in asynchronous systems (message queues, event queues).
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.