Queues Flashcards

1
Q

Deque

A

A double-ended queued. elements can be added to or removed from either the front (head) or back (tail)

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

How to pronounce deque

A

“Deck”

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

List two possible subtypes of deque

A

An input-restricted deque is one where deletion can be made from both ends, but insertion can be made at one end only.

An output-restricted deque is one where insertion can be made at both ends, but deletion can be made from one end only.

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

Shift

A

Common name for removing first element from a deque

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

Unshift

A

Common operation name in most languages for inserting an element into the front of a deque

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

Two most common ways to efficiently implement a deque

A

Using an array deque or a doubly linked list

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

Circular buffering makes a good implementation strategy for what?

A

A queue with a fixed maximum length

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

The useful property of a circular buffer

A

is that it does not need to have its elements shuffled around when one is consumed in a FIFO manner. (If a non-circular buffer were used then it would be necessary to shift all elements left when one is consumed. (Because the buffer is contiguous memory and you need to keep space open at end of the buffer))

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

Describe how a circular buffer works

A

Fixed length buffer (represented by say a starting pointer and ending boundary pointer) then with two more logic pointers (or indices) representing the start and end of the logical portion (as opposed to physical portion) of the queue. On every insert move the end logical pointer one forward and put element there ( if at end of buffer, loop around to the starting boundary pointer pointer). On remove operations, just return the value at the starting logical pointer (thus performing FIFO logic) and then increment the starting logical pointer one slot)

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

When may you not want to use a circular buffer for a queue? And what implementation may you want for a queue instead?

A

For arbitrarily expanding queues, because reallocating new memory for a larger buffer and copying over the data will take O(n) time.

Instead use a linked list implementation

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

Double buffer analogy

A

One buffer = one bucket you fill in sink, turn sink off, walk outside to kiddie pool, fill up kiddie pool with bucket , walk back and fill up bucket again

Two buffer = two buckets. Filling on in sink while emptying one in pool

Three buffer = 3 buckets. One sink one in transport one filling pool

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

List the 3 common implementation of array deques

A

Storing deque contents in a circular buffer, and only resizing when the buffer becomes full. This decreases the frequency of resizings.

Allocating deque contents from the center of the underlying array, and resizing the underlying array when either end is reached. This approach may require more frequent resizings and waste more space, particularly when elements are only inserted at one end.

Storing contents in multiple smaller arrays, allocating additional arrays at the beginning or end as needed. Indexing is implemented by keeping a dynamic array containing pointers to each of the smaller arrays.

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

When would it be a really good idea to implement a deque using a circular buffer as opposed to a dynamic array strategy?

A

When insertions don’t outpace removals that often, such that queue length rarely exceeds the buffer size. When it does you’ll just have to create a new larger buffer which after copying values will still result in amortized constant time, though occasional one op linear time

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

ArrayDeque

A

A Java Implementation of a deque, using an array and two pointers

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