Q&A Flashcards

1
Q

Suppose you have a sorted list of 128 names, and you’re searching
through it using binary search. What’s the maximum number of
steps it would take?

A

7

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

Suppose you double the size of the list. What’s the maximum
number of steps now?

A

8

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

You have a name, and you want to find the person’s phone number
in the phone book.

A

O(log n)

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

You have a phone number, and you want to find the person’s name
in the phone book

A

O(n)

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

You want to read the numbers of every person in the phone book

A

O(n)

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

You want to read the numbers of just the As in a phonebook

A

O(n)

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

Suppose you’re building an app to keep track of your finances. Every day, you write down everything you spent money on. At the end of the month, you review your expenses and sum up how much you spent. So, you have lots of inserts and a few reads. Should you use an array or a list?

A

A linked list

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

Suppose you decided to use an array to store the list of users. What are the downsides of an array for inserts?

A

Inserting into arrays is slow

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

Suppose you accidentally write a recursive function that runs forever. What happens to the stack when your
recursive function runs forever?

A

The stack grows forever, When your program runs out of space, it will exit with a stack-overflow error

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

Write a recursive function to count the number of items in a list

A

def count(list):
if list == []:
return 0
return 1 + count(list[1:])

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

Write a recursive function to find the maximum number in a list.

A

def max(list):
if len(list) == 2:
return list[0] if list[0] > list[1] else list[1]
sub_max = max(list[1:])
return list[0] if list[0] > sub_max else sub_max

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

Can you come up with the base case and recursive case for binary search?

A

The base case for binary search is an array with one item, In the recursive case for binary search, you split the array in half, throw away one half, and call binary search on the other half.

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

Give the big O of Printing the value of each element in an array.

A

O(n)

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

Give the big O of Doubling the value of each element in an array.

A

O(n)

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

Give the big O of Doubling the value of just the first element in an array

A

Answer: O(1)

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

Creating a multiplication table with all the elements in the array. So if your array is [2, 3, 7, 8, 10], you first multiply every element by 2, then multiply every element by 3, then by 7, and so on.

17
Q

is this hash consistant
f(x) = 1

A

Consistent

18
Q

is this hash consistant
f(x) = rand()

A

Not Consistent

19
Q

is this hash consistant
f(x) = next_empty_slot()

A

Not Consistent

20
Q

is this hash consistant
f(x) = len(x)

A

Consistent