Queue Flashcards

1
Q

How would you describe a queue?

A
  • Usually First in first out
  • Some exceptions ie., Priority Queue
  • Add to front (enqueue)
  • Remove from rear (dequeue)
  • Size in the num of elements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When is a queue a good choice?

A

A queue is good when you want to get elements out in the same order as you put them in

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

What is a priority queue?

A

A Priority Queue is an implementation of the Queue interface that provides a priority ordering of elements.

It stores elements in a priority heap and allows elements to be removed in order of their priority.

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

Can you insert a NULL value into a queue?

A

No you shouldn’t, but technically you can insert NULL into LinkedList which implements the queue interface

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

What are the 3 most common implementations of the queue interface?

A

Priority Queue
LinkedList
Array Dequeue (Double Ended Queue)

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

What is the difference between a regular queue and a priority queue?

A

Each queue’s item has an additional piece of information, namely priority. Unlike a regular queue, the values in the priority queue are removed based on priority instead of the first-in-first-out (FIFO) rule.

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

What is the benefit of the queue?

A

You only need to keep track of two positions

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

What five queue operations are O(1)?

A
  • enqueue (add to end)
  • dequeue (remove from front)
  • peek
  • isEmpty
  • isFull

O(1) because items are added or removed from the ends

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

What would you need for an Array implementation of a queue that had variables, a constructor, enqueue, dequeue and a toString method?

A

public class ArrayQueue {
private int[] items;
private int rear;
private int front;
private int count;

public ArrayQueue(int capacity) {
items = new int[capacity];
}

public void enqueue(int item) {
if (isFull())
throw new IllegalStateException();

items[rear] = item;
rear = (rear + 1) % items.length;
count++;   }

public int dequeue() {
if (isEmpty())
throw new IllegalStateException();

var item = items[front];
items[front] = 0;
front = (front + 1) % items.length;
count--;

return item;   }

public int peek() {
if (isEmpty())
throw new IllegalStateException();

return items[front];   }

public boolean isEmpty() {
return count == 0;
}

public boolean isFull() {
return count == items.length;
}

@Override
public String toString() {
return Arrays.toString(items);
}
}

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

If we create a priority queue and add 5, 1, 3 and 2 to the queue in this order

If we loop through and remove each element in the queue, what order will the elements be printed?

A

1,2,3,5

because the default ordering of a java priority queue is ascending order

so if we added another value 4 it would not be added to the end, it would be added between 3 and 5

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

How do you reverse a queue?

A
  1. Create a stack
  2. While there are elements in the queue, remove each element from the queue, and push it to the stack
  3. While there are elements in the stack, pop the elements from the stack and add them to the queue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the most common implementations of the queue data structure in Java?

A

LinkedList - This is a linked list-based implementation of the Queue interface in Java. It provides O(1) time complexity for both adding and removing elements from both ends of the queue.

PriorityQueue - This is an implementation of the Queue interface that provides a priority ordering of elements. It stores elements in a priority heap and allows elements to be removed in order of their priority.

ArrayDeque - This is a double-ended queue (deque) implementation that allows elements to be added and removed from both ends of the queue. It provides constant-time performance for adding and removing elements from the beginning or end of the queue.

BlockingQueue - This is an interface that extends the Queue interface and provides additional methods for blocking operations. It allows a thread to wait until an element is available in the queue or until space is available for adding an element to the queue.

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

What is ArrayDeque?

A

Double Ended Queue (can add and remove from both sides)
Internally uses a resizable array

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