Algorithms Flashcards

1
Q

What is meant by time complexity of an algorithm?

A

How much time it takes to solve a particular problem inputted

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

Constant time complexity

A

O(1) Time taken is independent of the number of elements inputed

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

Linear time complexity

A

O(n) Time take is directly proportional to the number of elements inputted

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

Polynomial time complexity

A

O(n^n) Time taken is directly proportonal to the number of elements to the power of n

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

Exponential time complexity

A

O(2^n) Time taken will double as the input size increases by one

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

Logarithmic time complexity

A

O(log n ) Time taken will increase at a smaller rate than the number of elements inputted

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

Best to worst time complexities for an algorithm

A

best

o(1)
o(logn)
o(n)
o(nlogn)
o(n^2)
o(2^n)

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

What is meant by the space complexity of an algorithm?

A

The amount of storage an algorithm takes

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

What is the time complexity of linear search?

A

O(n) linear

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

What is the time complexity for binary search?

A

O(log n) logarithmic

Because the size of the list is halved every step

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

Give an example of a divide and conquer algorithm?

What does this mean for an algorithm?

A

Binary search or merge sort

Binary: Splits the list into half
Conquer: It recursively/ iteratively searches in one of the halves based on a comparison (whether the target is less than, greater than, or equal to the middle element).
Combine: In the case of binary search, there’s no explicit “combine” step, but the result of each list division which eventually leads to the target or that it’s not present.

Mergesort:
Divide: Split lists in half until each contains just one element
Conquering each subproblem recursively (ordering).
Combining the results of the subproblems to get the final solution.

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

What is the time complexity of merge sort?

A

O(nlogn)

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

What would the top pointer of a stack be initialised to?

Why?

A

-1 because the first element in the stack is in possition 0. Having the pointer initialised at 0 would suggest there is already an element in the stack even though the stack is empty

(item is inserted into pointer+1 index)

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

Pseudocode for function which returns number of elements on a stack

A

size()
return top +1

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

Pseudocode for function which returns number of elements on a queue

A

size()
return back - front

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

What is a linked list?

A

-A data structure composed of nodes, each of which has a pointer to the next node in the sequence.
(Unlike arrays, linked lists do not store elements in contiguous memory locations)
each node contains two parts:

Data: The value or data that the node holds.
Next (or Pointer/Link): A reference/pointer to the next node in the list. If it’s the last node, this pointer is null

-First item in a list is called the head and the last is the tail

E.g if a node is called N then the next node is referred to as N.next

17
Q

Define tree

A

A tree is a connected, undirected graph that contains no cycles

18
Q

What is the time complexity for the bubble sort algorithm?

19
Q

Depth first traversal

A

Go as far down into the tree as possible before backtracking
by Goes to the left child node of the current node
If there is no left child print node then go to right child and output

If there are no child nodes, visit current node and output value then backtrack to the next node on stack and moving right output
Uses a stack

(like postorder traversal)

20
Q

Breath first traversal

A

Starting from left, visits all children of start node
If one has to children then backtracks

Uses a queue

21
Q

Breath first traversal uses a stack

TRUE OR FALSE?

A

FALSE

Uses a queue

22
Q

What data structure does depth first traversal use>?

23
Q

What is the time complexity of insertion sort?

24
Q

What is the time complexity of merge sort?

25
Q

Bubble sort is more efficient than insertion sort and merge sort

TRUE OR FALSE

A

FALSE

Merge sort is more efficient than bubble sort and insertion sort

Merge O(nlogn)
Insertion and bubble O(n^2)

26
Q

What is the time complexity of quick sort?

27
Q

Binary search can be applied on sorted and unsorted data

TRUE OR FALSE?

A

FALSE

Only applied on sorted data

28
Q

The time complexity of binary search is O(n^2)

TRUE OR FALSE?

A

FALSE

O(logn)

29
Q

What is the time complexity of linear search?

30
Q

The A* algorithm has 2 cost functions

What are they?

A
  1. Actual cost between two nodes (same as in Dijkstras)
  2. Approximate cost from node x to the final node (called a heuristic)(might be the estimate length between x and the final node, calc using trig)