Cracking the Coding Interview Flashcards

(11 cards)

1
Q

Big O vs Big Omega vs Big Theta

A

In academia, Big O is upper bound of time, Big Omega is lower bound, and Big Theta is combined both upper and lower bound of time

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

Space complexity

A

Similar to Big O and time complexity, how much memory something needs to allocate to run.

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

What do you need to do with O(2N)

A

Drop the constant, O(2N) = O(N)

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

In addition to dropping constants, when using Big O you should also

A

Drop non dominant terms:
O( N^2 + N) => O(N^2)
O(N + logN) => O(N)
O(5*2^N + 1000N^100) => O(2^N)

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

When using Big O, when to add and when to multiply?

A

If “do this, then when youre done do that” add. If “do this for each time you do that” then multiply.

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

What is amortized time?

A

Sometimes things take multiple different times. With an array list, it is O(1) unless the array needs to be expanded, at which case it will take O(N) time. When it is expanded the size is doubled.

The doubling happens in powers of 2, so at elements 1,2,4,8,16…X. The sum of this is x + x/2 + x/4 … + 1 = 2x.

Therefore, X insertions take O(2X) time, but the amortized time for each insertion is O(1).

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

Where do logN runtimes come from?

A

Anytime you cut the number of steps in half each time (think binary search)

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

What is the general rule for BigO for recursive functions that make multiple calls?

Ex: return f(n-1) + f(n-1);

A

The runtime will often, though not always, be O(branches^depth) where branches is the number of times each recursive call branches, in the case from this question that would be 2. So O(2^N).

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

In bigO, the base of an exponent (does/does not) matter?

A

It does matter, 2^N != 3^N.

For ex: 8^N = 2^3^N = 2^3n = 2^2N * 2^N and 2^N is not a constant factor so it is not dropped.

In contrast, the base of logs does not matter. We dont care about log base 7 n, we just say logN.

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

With bigO, be sure not to use N

A

in multiple ways. If asked for the runtime to sort strings in an array, then sort the array itself, you cant use N for both the length of the strings and the length of the array.

Define terms: s = length of string, a = length of array.

To sort each string = O(s log s)
Need to do for each string in array gets us to O(a*s * log s)

Sort strings in array (dont forget you need the time to compare each string which is O(s) time) is O(a*s log a)

Add it all together and you get O(a*s (log a + log s))

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