Arrays and Lists Flashcards

1
Q

What is an implementation of a shuffle algorithm?

A

This is an implementation of the Inside-Out variation of the Fisher-Yates shuffle:

To initialize an array a of n elements to a randomly shuffled copy of “source”, both 0-based

for i from 0 to n - 1 do:
j = random integer such that 0 <= j <= i
if j != i, then a[i] = a[j]
a[j] = source[i]

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

Time complexity of python list copy

A

O(n)

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

Time complexity of python list append[1]

A

O(1)

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

Time complexity of python list insert

A

O(n)

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

Time complexity of python list get item

A

O(1)

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

Time complexity of python list set item

A

O(1)

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

Time complexity of python list delete item

A

O(n)

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

Time complexity of python list get slice[k]

A

O(k)

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

Time complexity of python list del slice

A

O(n)

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

Time complexity of python list set slice[k]

A

O(k+n)

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

Time complexity of python list extend

A

O(k)

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

Time complexity of python list sort

A

O(n log n)

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

Time complexity of python list multiply []*k

A

O(nk)

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

Time complexity of python list x in s

A

O(n)

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

Time complexity of python list min/max

A

O(n)

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

Time complexity of python list get length

A

O(1)

17
Q

Time complexity of python dict get item

A

O(1), amortized worst case O(n)

18
Q

Time complexity of python dict set item

A

O(1), amortized worst case O(n)

19
Q

Time complexity of python dict delete item

A

O(1), amortized worst case O(n)

20
Q

what is the algorithm for rotating an array left by r?

What is the complexity of the algorithm, and what other algorithmic options are there?

A
The simplest possible way to rotate an array can be done with a copy.  Time O(N), Space O(N), which is not that bad.
def rotate_copy(s, r):
    N = len(s)
    out = [None]*N
    for i in range(0, len(s)):
        out[i] = s[(i+r)%N]
    return out

In-place rotation is a bit more complicated, but uses slightly less space. However, a simple inplace algorithm still requires O(N) space

We can get space down to O(1) by using the Juggling algorithm, which requires a constant temporary space. The juggling algorithm might come in handy in the case where you want to visit every item in a particular order of fixed skips, like a rotation.