Queues Flashcards

(51 cards)

1
Q

What is a queue?

A

A linear data structure that follows the First In, First Out (FIFO) principle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is FIFO?

A

First In, First Out — the first element added is the first one to be removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the basic operations of a queue?

A

Enqueue, dequeue, peek (or front), and isEmpty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the enqueue operation do?

A

Adds an element to the rear of the queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the dequeue operation do?

A

Removes and returns the front element of the queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the peek (or front) operation do?

A

Returns the front element without removing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the isEmpty operation do?

A

Checks whether the queue is empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the time complexity of enqueue and dequeue operations?

A

O(1) in a properly implemented queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can a queue be implemented?

A

Using arrays, linked lists, or two stacks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a circular queue?

A

A queue that connects the end back to the beginning to efficiently use memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is a circular queue implemented?

A

Using a fixed-size array with front and rear pointers that wrap around.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What causes queue overflow?

A

Adding an element to a full queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What causes queue underflow?

A

Removing an element from an empty queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a priority queue?

A

A queue where elements are dequeued based on priority rather than order of insertion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is a priority queue typically implemented?

A

Using a heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a double-ended queue (deque)?

A

A queue where elements can be added or removed from both the front and rear.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the difference between a queue and a deque?

A

A queue is FIFO; a deque allows insertion and deletion at both ends.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the time complexity of accessing the middle element in a queue?

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the space complexity of a queue with n elements?

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are common use cases for queues?

A

Scheduling, buffering, breadth-first search (BFS), producer-consumer problems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How is a queue used in BFS?

A

To track nodes to visit in order of discovery.

22
Q

What is a bounded queue?

A

A queue with a fixed maximum capacity.

23
Q

What is an unbounded queue?

A

A queue that can grow as needed, limited only by available memory.

24
Q

What is the main disadvantage of a queue implemented with a static array?

A

It wastes space when elements are dequeued unless circular logic is used.

25
What is a real-world example of a queue?
A line of customers at a store checkout.
26
How do you implement a queue using two stacks?
Push to one stack for enqueue; for dequeue, transfer elements to the second stack.
27
How do you implement a queue using a linked list?
Keep track of both head (front) and tail (rear) nodes.
28
What is the difference between stack and queue?
Stack is LIFO; queue is FIFO.
29
How is peek operation different from dequeue?
Peek returns the front element without removing it; dequeue removes it.
30
Can a queue have duplicate elements?
Yes, queues allow duplicates.
31
Can a queue be used for reversing data?
Not efficiently — a stack is better for reversing.
32
What is the role of queues in operating systems?
Used in task scheduling, buffering, and managing requests.
33
How is a message queue used in concurrent programming?
For communication between threads or processes.
34
What is a blocking queue?
A queue where operations block if the queue is full or empty.
35
What is a non-blocking queue?
A queue where operations return immediately without waiting.
36
What is a monotonic queue?
A deque that maintains elements in sorted order, useful for sliding window problems.
37
What is the benefit of a circular queue over a linear queue?
It avoids unused space and efficiently reuses array slots.
38
How do you handle resizing in dynamic queue implementations?
Create a larger array and copy elements in correct order.
39
What is the rear pointer in a queue?
It points to the position where the next element will be added.
40
What is the front pointer in a queue?
It points to the current first element to be dequeued.
41
What happens when front equals rear in a queue?
The queue is either empty or full depending on implementation.
42
What is the use of a deque in sliding window problems?
It efficiently tracks maximum or minimum in each window.
43
What does it mean to rotate a queue?
Move the front element to the rear.
44
What is the dequeue operation in a circular queue?
Remove the front element and advance the front pointer with wrap-around.
45
What is the role of a queue in graph algorithms?
Used in level-order traversal and shortest path in unweighted graphs.
46
What is a command queue?
A queue that holds operations to be executed in order.
47
What are sentinel values in queues?
Special values used to mark boundaries or end conditions.
48
What is the role of a queue in producer-consumer problems?
Acts as a buffer between producing and consuming threads.
49
What is the benefit of using a linked list for a queue?
It allows dynamic sizing without shifting elements.
50
What is a simple algorithm that uses a queue?
Breadth-First Search (BFS) in trees or graphs.
51
What happens when a queue is empty and dequeue is called?
An underflow condition or error occurs.