Queues Flashcards
(51 cards)
What is a queue?
A linear data structure that follows the First In, First Out (FIFO) principle.
What is FIFO?
First In, First Out — the first element added is the first one to be removed.
What are the basic operations of a queue?
Enqueue, dequeue, peek (or front), and isEmpty.
What does the enqueue operation do?
Adds an element to the rear of the queue.
What does the dequeue operation do?
Removes and returns the front element of the queue.
What does the peek (or front) operation do?
Returns the front element without removing it.
What does the isEmpty operation do?
Checks whether the queue is empty.
What is the time complexity of enqueue and dequeue operations?
O(1) in a properly implemented queue.
How can a queue be implemented?
Using arrays, linked lists, or two stacks.
What is a circular queue?
A queue that connects the end back to the beginning to efficiently use memory.
How is a circular queue implemented?
Using a fixed-size array with front and rear pointers that wrap around.
What causes queue overflow?
Adding an element to a full queue.
What causes queue underflow?
Removing an element from an empty queue.
What is a priority queue?
A queue where elements are dequeued based on priority rather than order of insertion.
How is a priority queue typically implemented?
Using a heap.
What is a double-ended queue (deque)?
A queue where elements can be added or removed from both the front and rear.
What is the difference between a queue and a deque?
A queue is FIFO; a deque allows insertion and deletion at both ends.
What is the time complexity of accessing the middle element in a queue?
O(n)
What is the space complexity of a queue with n elements?
O(n)
What are common use cases for queues?
Scheduling, buffering, breadth-first search (BFS), producer-consumer problems.
How is a queue used in BFS?
To track nodes to visit in order of discovery.
What is a bounded queue?
A queue with a fixed maximum capacity.
What is an unbounded queue?
A queue that can grow as needed, limited only by available memory.
What is the main disadvantage of a queue implemented with a static array?
It wastes space when elements are dequeued unless circular logic is used.