ArrayDeque Flashcards

1
Q

What is the runtime of an ArrayDeque get(i) operation?

A

O(1)

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

What is the runtime of an ArrayDeque set(i,x) operation?

A

O(1)

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

What is the runtime of an ArrayDeque add(i,x) operation?

A

O(min{i, n-i}), amortized.

Recall that since the ArrayDeque is implemented by a circular array, you make space for item i by either shifting (i-1) items to the left, or (n-i+1) items to the right, whichever is smaller.

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

What is the runtime of an ArrayDeque remove(i) operation?

A

O(min{i, n-i}), amortized.

Recall that since the ArrayDeque is implemented by a circular array, once you remove item i you can either shift the (i-1) items preceding i to the right, or shift the (n-i) items following i to the left, whichever is smaller.

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

How much extra space is needed for an ArrayDeque to store n elements?

A

O(n) extra space.

An ArrayDeque uses an array as backing storage, and that means that our array can be as much as 3 times the size of the elements it is storing, before we resize it to twice the number of elements.

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

What index i leads to the worst-case running time for the add(i,x) operation in an ArrayDeque?

A

i=n/2, that is, adding to the middle of an ArrayDeque is the worst.

This is because min{i, n-i} is maximized when i=n/2.

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

Pros of an ArrayDeque

A

Good (O(1) amortized) add and remove from both ends, which makes it a good implementation of the Deque interface.
Fairly straight-forward implementation.
Good (O(1)) get and set methods, so good for random access.

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

Cons of an ArrayDeque

A

Amortized time, which isn’t always ideal.

O(n) extra space used when storing n elements.

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

What index i leads to the best-case runtime of the ArrayDeque add(i,x) operation?

A

Either i=0 or i=n-1.

These lead to O(1) runtimes.

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

What makes the runtimes of the ArrayDeque’s add(i,x) and remove(i) operations amortized?

A

The copying in the resize() method.

(Arrays are of fixed length; therefore, any array-based List implementation must occasionally resize this array as we need more space. When we resize, we create a new array and copy all n elements from the original array into the new array, and this copying takes O(n) time that is amortized over all adds/removes that happened since the last add/remove.)

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

What is the backing storage of an ArrayDeque?

A

an array a to store the elements
an integer n for the size
an integer j to store the index of the first element

(a.k.a. a “circular array”)

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