Space and Time Complexity Flashcards

(47 cards)

1
Q

What is time complexity?

A

A measure of the amount of time an algorithm takes as a function of input size.

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

What is space complexity?

A

A measure of the amount of memory an algorithm uses as a function of input size.

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

What is Big O notation?

A

A mathematical notation used to describe the upper bound of an algorithm’s runtime or space usage.

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

What does O(1) mean?

A

Constant time or space — independent of input size.

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

What does O(n) mean?

A

Linear time or space — grows directly with input size.

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

What does O(log n) mean?

A

Logarithmic time or space — grows slowly even for large inputs.

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

What does O(n log n) mean?

A

Linearithmic complexity — typical of efficient sorting algorithms.

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

What does O(n^2) mean?

A

Quadratic time or space — often results from nested loops.

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

What does O(2^n) mean?

A

Exponential complexity — grows very quickly with input size.

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

What does O(n!) mean?

A

Factorial time — extremely slow, used in brute-force permutations.

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

What is worst-case complexity?

A

The maximum amount of time or space an algorithm can take.

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

What is best-case complexity?

A

The minimum amount of time or space an algorithm can take.

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

What is average-case complexity?

A

The expected amount of time or space across typical inputs.

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

What is amortized complexity?

A

The average time per operation over a sequence of operations.

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

What is auxiliary space?

A

Extra space or temporary space used by an algorithm.

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

What is in-place algorithm?

A

An algorithm that uses only a constant amount of extra space.

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

What is the time complexity of binary search?

A

O(log n)

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

What is the time complexity of linear search?

A

O(n)

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

What is the space complexity of recursive algorithms?

A

O(depth of recursion) due to call stack.

20
Q

What is the time complexity of quicksort on average?

21
Q

What is the worst-case time complexity of quicksort?

22
Q

What is the time complexity of merge sort?

A

O(n log n) in all cases.

23
Q

What is the space complexity of merge sort?

A

O(n) due to auxiliary arrays.

24
Q

What is the time complexity of heap sort?

25
What is the space complexity of heap sort?
O(1) for in-place version.
26
What is the time complexity of inserting into a heap?
O(log n)
27
What is the time complexity of BFS or DFS using adjacency list?
O(V + E)
28
What is the space complexity of BFS?
O(V) for queue and visited set.
29
What is the time complexity of Dijkstra’s algorithm with a min-heap?
O((V + E) log V)
30
What is the space complexity of storing a graph with adjacency matrix?
O(V^2)
31
What is the time complexity of finding an element in a hash table?
O(1) average, O(n) worst-case.
32
What is the space complexity of a hash table?
O(n)
33
What is the complexity of building a trie with n words of length m?
O(n * m)
34
What is the complexity of searching in a trie?
O(m), where m is the length of the key.
35
What is the time complexity of finding union in disjoint set with path compression?
O(α(n))
36
What is α(n) in time complexity?
The inverse Ackermann function — grows very slowly.
37
What is the difference between Big O, Omega, and Theta?
O is upper bound, Ω is lower bound, Θ is tight bound.
38
What is a tight bound?
An exact asymptotic behavior, expressed using Theta notation.
39
What is the time complexity of matrix multiplication?
O(n^3) for naive method.
40
What is the best-case time complexity of bubble sort?
O(n) when the array is already sorted.
41
What is the worst-case time complexity of bubble sort?
O(n^2)
42
What is the time complexity of inserting into a BST?
O(log n) for balanced trees.
43
What is the worst-case insertion time for unbalanced BST?
O(n)
44
What is the space complexity of an iterative algorithm?
Usually O(1) unless it uses additional data structures.
45
Why is analyzing time complexity important?
To predict how an algorithm scales with input size.
46
Why is analyzing space complexity important?
To ensure efficient memory usage, especially with large inputs.
47
What is the impact of nested loops on time complexity?
Each level of nesting multiplies the complexity, often resulting in O(n^2), O(n^3), etc.