Algorithms Flashcards

1
Q

How does binary search work

A

Finds the middle element in a list of data before deciding which side of the data the desired element is to be found in. The unwanted half of the data is then discarded, and the process is repeated until the desired element is found or until it is known that the desired element
doesn’t exist in the data.

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

What must be true about a list for binary search to be true

A

The list must have been sorted already

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

How does linear search work

A

You can think of it as going along a
bookshelf one by one until you come across the book you’re looking for. Sometimes the
algorithm gets lucky and finds the desired element almost immediately, while in other
situations, the algorithm is incredibly inefficient.

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

What is the order for post order traversal

A

Left right root

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

What is the order for breadth first traversal

A

from top to bottom, left to right

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

How does bubble sort work

A

The bubble sort algorithm works by repeatedly going through a list of items, comparing consecutive pairs of items and swapping the items if they are in the wrong order.

Each time the algorithm goes through the list, it is called a pass and on the final pass, it checks to see if any swaps are needed, if not the list must be sorted.

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

How does insertion sort work

A

The insertion sort algorithm works by building up a sorted sub list in the first part of the original list, while the remaining part of the list remains unsorted

At the start of the algorithm, the sorted sub list contains just a single item (the first item in the list). All of the other items belong to the unsorted sub list. The algorithm goes through the unsorted sub list, item by item. As each item is examined, it is moved into the correct position (in ascending or descending order) in the sorted sub list. This progresses until the final item is correctly inserted and the list is sorted

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

Explain one situation where insertion sort would be better than bubble sort

A

On large lists, as it usually requires less comparisons per pass making it quicker and more efficient

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

Describe the stack data structure

A

A stack is a type of FILO data structure, using a pointer to point to the element currently at the top of the stack

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

Describe the queue data structure

A

A queue is a type of FIFO data structure, using two pointers.

A front pointer to points to the first element in the queue and a back pointer which points to the next empty space at the back of the queue

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

Give 1 similarity and one difference between stacks and queues

A

Similarity: Both are often implemented as arrays

Difference: stacks use 1 pointer, queues use 2

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

What operation are used to add and remove elements from a queue

A

Add: enqueue(elementName)
remove: dequeue()

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

What operation are used to add and remove elements from a stack

A

Add: push(elementName)
remove: pop()

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

What happens to the pointer in a stack when the stack is popped

A

The pointer decrements by 1, pointing to the element below

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

What happens to the pointers in a queue when the queue is dequeued

A

The front pointer incremented by 1, pointing to the next element in the list

The back pointer remains unchanged

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

For both stacks and queues, what must be checked before trying to remove elements

A

The stack or queue has to be checked to see if its empty, if not then an element can be removed

17
Q

What happens to the back pointer in a queue if an element is added

A

The back pointer increments by one

18
Q

What is meant by time complexity

A

Time complexity is the amount of time it takes an algorithm to solve a particular problem

19
Q

What is meant by constant time complexity

A

The amount of time taken to complete an algorithm is independent from the number of elements inputted
O(1)

20
Q

What is meant by linear time complexity

A

The amount of time taken to complete an algorithm is directly proportional to the number of elements inputted
O(n)

21
Q

What is meant by polynomial time complexity

A

The amount of time taken to complete an algorithm is directly proportional to the square of the elements inputted
O(n^2)

22
Q

What is meant by exponential time complexity

A

The amount of time taken to complete an algorithm will double with every additional item
O(2^n)

23
Q

What is meant by logarithmic time complexity

A

The time taken to complete an algorithm will increase at a smaller rate as the number of elements inputted

24
Q

What is meant by space complexity

A

Space complexity is the amount of storage the algorithm takes up

25
Q

What is the worst and best case time complexity of linear search

A

worst: O(n)
best: O(1)

26
Q

What is the worst and best case time complexity of binary search

A

worst: O(log n)
best: O(1)

27
Q

What is the worst and best case time complexity of bubble sort

A

worst: O(n^2)
best: O(n)

28
Q

What is the worst and best case time complexity of insertion sort

A

worst: O(n^2)
best: O(n)

29
Q

What is the worst and best case time complexity of merge sort

A

worst: O(nlogn)
best: O(nlogn)

30
Q

What is the worst and best case time complexity of quick sort

A

worst: O(n^2)
best: O(nlogn)

31
Q

Describe the tree data structure

A

A tree is a connected, undirected graph with no cycles.

32
Q

What is meant by a tree being undirected

A

Undirected means that there is no direction associated with an edge.

33
Q

What is meant by a tree being connected

A

Connected means that there is a path from any node to any other node, and there is no node, or set of nodes, that is disconnected from the others

34
Q

What is a ‘root’ in the tree data structure

A

The root of a tree is the start node for traversals. If the tree has a root, it is called a rooted tree.

35
Q

What is a ‘branch’ in the tree data structure

A

A branch is a path from the root to an end point. The end point is called a leaf.

36
Q

In a rooted tree data structure, what is a leaf

A

A leaf is a node with no children.

37
Q

Describe the structure of a binary tree

A

A binary tree is a rooted tree where every node has at most two child nodes.

38
Q

Describe the structure of a linked list

A

A linked list is a dynamic database structure.

Each element in a linked list is called a node. Each node stores The data relating to the element and a pointer to the next node

39
Q

What does the last elements pointer in a linked list point to

A

null