Time/Space Complexity Flashcards

1
Q

O(1)

A

Constant time. Most efficient algo

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

Big O of pop/remove from hashmap

A

O(1)

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

Big O of lookup in hashmap

A

O(1)

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

Big O of search an array

A

Worse case: O(n)

Might have to go through entire array to find

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

Big O of insert into hashmap

A

O(1)

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

Big O of loop through an array

A

O(n)

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

Big O of push/pop from array

A

O(1)

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

What is O(log n)

A

Binary search.

On every iteration of loop, we halve the remaining elements and search again

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

What is O(n^3)

A

3-D data structure. Get triplets of loops

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

What is O(n * m)

A

2-D array that’s not a perfect square (ex. 2x3 grid)

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

Big O of getting length of list

A

O(1)

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

Big O deque vs. list

A

Index access:
- Deque: O(n)
- List: O(1)

Pop from right:
- Deque: O(1)
- List O(1)

Pop from left:
- Deque: O(1)
- List: O(n)

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

Big O of sum an array

A

O(n)

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

What is O(n!)

A

Permutations
Ex. traveling salesman

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

What is O(n log n)

A

Heap sort, merge sort

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

What is O(2^n)

A

Recursion, with tree height of n and 2 branches.

Can be 4^n, 5^n, etc. Depends on how many loops for recursion

17
Q

Sort a list

A

O(n log n)

18
Q

What is O(n^2)

A

Traverse a square grid. Get every pair of elements in an array

19
Q

Big O of remove/insert from middle of array

A

Worse case: O(n) since values have to be moved around

20
Q

What is O(n)

A

Linear growth/time. As input grows, so does time

21
Q

Big O of lookup in array

A

O(1)

22
Q

Difference between O(n + m) and O(n * m)

A

O(m+n) example:

for(int i = 0, i < m, i++)
//code
for(int j = 0, j < n, j++)
//code

m iterations of code happen. Then n iterations of code happens.

O(mn) example:

for(int i = 0, i < m, i++)
for(int j = 0, j < n, j++)
//code

For every iteration of m, we have n iterations of code. Imagine iterating over a non-square 2D array.

m and n do not necessarily equal the same value. If they did equal the same value, then for O(m+n):

O(m+n) => O(m+m) => O(2m) => O(m)

23
Q

When to add vs multiply for Big O setup

A

Add different complete instructions (loop through a, when done with all, loop through b)

Multiple if nested loop

24
Q

Big O of list extend()

A

The extend() operation here is O(k) where k=4 (the size of list2).

list1 = [1, 2, 3]
list2 = [4, 5, 6, 7]
list1.extend(list2)

25
Q

Big O of:

class Solution:
    def getConcatenation(self, nums: List[int]) -> List[int]:
        result = []
        for i in range(0, 2):
            result.extend(nums)
        return result
A

O(n)

range(0, 2) means we iterate twice, so O(2)

We extend the nums, which would be O(n), and we do it twice, so it would be O(2n)

We drop constants so final is O(n)

26
Q

Big O of python split()

A

Splitting a string into words can be considered an O(s) operation, since it needs to examine each character to determine where to split.

27
Q

Big O of max() on list

A

O(n)

28
Q

Big O of min() on list

A

O(n)

29
Q

Big O of remove() on list

A

O(n)

30
Q

Big O of extend() on list

A

O(k) where k is how many items are being added to the list

31
Q

Time complexity of:

class Solution:
    def decompressRLElist(self, nums: List[int]) -> List[int]:
        result = []
        for i in range(0, len(nums), 2):
            result.extend([nums[i + 1]] * nums[i])
        return result
A

O(n x m)

O(n) for the ‘i in range’ part, and O(m) for extend(). Since it is nested, it is multiplied

32
Q

Big O of ‘ ‘.join(list)

A

The time complexity of the join_string() function is O(n), where n is the total length of the input list of strings list_string, because the str. join() method iterates over each string in the list and concatenates them with a hyphen delimiter.

33
Q

Big O of using dict(zip(list1, list2))

A

O(min⁡(m,n))

When you use zip(list1, list2), it pairs up corresponding elements from the two lists until one of the lists runs out of elements. The time complexity of this operation is O(min⁡(m,n)), where m is the length of list1 and n is the length of list2.

When converting the zipped pairs into a dictionary using dict(…), Python will iterate over the zipped pairs and insert each into the dictionary. Inserting an item into a dictionary is, on average, an O(1) operation. However, since this operation is done for each item, the overall time complexity becomes O(min⁡(m,n)).

34
Q

Big O of comparing two strings

A

Comparing two strings has a time complexity of O(k), where k is the length of the shorter string.

35
Q

Big O of comparing two lists of varying lenghts

A

The complexity of comparing two lists is O(n) if both lists have length n, and O(1) if the lists have different lengths

36
Q

Big O of comparing two lists that represent alphabet ([0] * 26)

A

These lists are always of the same length (both are of length 26, since they represent counts of the 26 lowercase English letters). So the complexity for comparing them would be O(26) which is a constant time and can be represented as O(1).