Data Structures and Algorithms Flashcards

1
Q

What is a data structure?

A

A conceptual way of arranging data

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

What is the difference between a static and dynamic data structure?

A

Static:
- Stored contiguously in memory as a series of sequential memory locations
- Storage size is fixed in compilation

Dynamic:
- Changing storage size during run time
- Store pointers as well as data to point between to next memory location
- Data dynamically allocated from the heap

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

What is overflow in static data structures?

A

Attempting to store more data than is available with set memory locations.

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

What data structures are static and which are dynamic?

A

Static:
- Arrays
- Some stacks
- Some queues
- Graphs which use adjacency matrix

Dynamic:
- Some stacks
- Some queues
- Graphs which use an adjacency list
- Lists

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

What is an array?

A

A finite, indexed set of related elements of the same data type.

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

What is a stack?

A

An abstract data structure which operates on a FILO (first-in-last-out) basis

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

What components are there in a stack?

A

Stack array / list - stores the items in the stack
Stack pointer - indicates the top of the stack

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

What are the key operations we can perform on a stack?

A

1) Push (add item to top of stack)
2) Pop (remove item from top of stack)
3) Peek (return the value at the top of the stack)
4) Checking if a stack is empty
5) Checking if a stack is full

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

How can you check if a stack is empty?

A

You can check if the index of the stack pointer is at -1 (initial value).
~~~
private bool IsEmpty() {
return stackPointer == -1;
}
~~~

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

How to check if a stack is full?

A

Check if the index of the stack pointer is at the max size of the stack - 1;
~~~
private bool IsFull() {
return stackPointer == stack.Length - 1;
}
~~~

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

How to push an item onto the stack?

A

1) Check if the array storing the stack is full. If it is, raise an error / exception.
2) If not full, increment the stack pointer by 1.
3) Store the item at the new memory that the stack pointer points to.
~~~
public int[] Push(int x) {
if (!IsFull()) {
stackPointer++;
stack[stackPointer] = x;
return stack;
} else {
System.Console.WriteLine(“The Stack is full.”);
return stack;
}
}
~~~

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

How to pop an item off the stack?

A

1) Check if the array storing stack is empty. If empty, raise an error or throw an exception
2) If not, decrement the stack pointer by 1.
3) Return the value of the item at index stack pointer + 1.
~~~
public void Pop() {
if (!IsEmpty()) {
stackPointer–;
System.Console.WriteLine(stack[stackPointer + 1] + “ has been popped from the stack.”);
} else {
System.Console.WriteLine(“The list is empty! Cannot pop!”);
}
}
~~~

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

How to peek an item in a stack?

A

1) Check if array storing stack is empty. If it is, raise an error
2) Return value of item at index of stack pointer.
~~~
public int? Peek() {
if (!IsEmpty()) {
System.Console.WriteLine(“Your number is “ + stack[stackPointer]);
return stack[stackPointer];
} else {
System.Console.WriteLine(“Stack is empty! Nothing to peek!”);
return null;
}
}
~~~

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

What is a multi-dimensional array?

A

an n-dimensional array is a set of elements of the same data type with each element being of type array with n-1 dimension

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

What is an abstract data stucture?

A

An unimplemented data structure which makes use of other data structures to form a new way of storing data. (e.g. stacks using arrays)

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

Key differences between static and dynamic data structures

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

What is a queue?

A

An abstract data structure that operates on a FIFO (first-in-first-out) basis.

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

What are the types of queues?

A

1) Linear Queues
2) Circular Queues
3) Priority Queues

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

What are the elements of a linear queue?

A
  • Array or list for the items
  • Front and rear pointer (for start and end of the queue)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the key operations of a linear queue?

A

1) Check if the array for the queue is empty
2) Check if the array for the queue is full
3) Enqueue (add) an item to the end of the queue
4) Dequeue (remove) an item from the start of the queue

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

How do you check if a LINEAR queue’s array is empty?

A

Check if the value of rear pointer is less than the value of the front pointer
~~~
public bool IsEmpty() {
if (rearPointer < frontPointer) {
return true;
else {
return false;
}
}
~~~

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

How to check if a queue is full?

A

Check if the index of the rear pointer is at the max size - 1.
~~~
public bool IsFull() {
if (rearPointer == maxSize - 1) {
return true;
else {
return false;
}
}
~~~

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

What are the advantages and disadvantages of linear queues?

A

Advantages:
- Simple to program

Disadvantages:
- Can lead to wasted capacity due as memory is stored statically (static)
- Process of shuffling is inefficient

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

What is a circular queue?

A

A type of queue that allows the front and rear pointer over the ends of the queue. This makes it a more efficient structure.

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

What mathematical operation do we use in implementation of a circular queue for indexing?

A

Modulo function e.g. with enqueue
~~~
public void Enqueue(T toAdd) {
if (!isFull()) {
rearPointer = (rearPointer ++) % maxSize;
items[rearPointer] = toAdd;
queueSize++;
} else {
System.Console.WriteLine(“The queue is full!”);
}
}
~~~

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

What is a priority queue?

A

A queue that has each item assigned with a priority with items with the highest priority being dequeued before low priority happen.

Within a given priority, items are dequeued in a first-in-first-out basis.

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

What are the uses for queues and stacks?

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

What is a recursive subroutine?

A

A subroutine that is defined in terms of itself

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

What is the role of the stack when a recursively-defined subroutine is exected?

A
  • store parameters
  • store return addresses
  • store local variables / return values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is used to see the priority of an element in a queue and what does it represent in implementation?

A

An integer used for priority
It represents which array of the 2d array to add the element to

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

What is a graph?

A

An abstract data structure to represent complex relationships between items within datasets.

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

What does a graph consist of?

A

1) Nodes (vertices) - represent the elements between which the relationships exist
2) Edges (arcs) - represents the relationship between two interconnected nodes

33
Q

What is meant by a weighted graph?

A

The edges are assigned a value which can represent time, distance or cost.

34
Q

Why are adjacency matrices and adjacency lists used?

A

Both are used to represent graphs in the memory of a computer.

35
Q

How does a adjacency matrix represent a graph?

A
  • A table of size n x n where is the number of nodes in the graph
  • Adjacency matrices adopt a tabular representation where an edge can be shown to exist using a 1 and a lack of an edge with a 0.
  • Weighted graphs can alse assign different values dependent on weight of an edge.
36
Q

How does an adjacency list represent a graph?

A

Stores nodes in a list, with each node holding its value along with a list of its adjacent nodes.

37
Q

What are the advantages and disadvantages of an adjacency matrix?

A

+ves:
- Time efficient: can query a specific edge quickly as you can look up row and column
- Suitable for dense graphs

-ves:
- Memory inefficient: stores information about every possible edge, not just those that exist

38
Q

What are the advantages and disadvantages of adjacency lists?

A

+ves:
- Memory efficient: only stores edges that exist
- Suitable for sparse graphs (few edges)

-ves:
- Time inefficient: slow to query an edge as each item in list of adjacent nodes must be searched sequentially

39
Q

What is a tree?

A

A connected, undirected graph with no closed loops

40
Q

What traversal methods are used to traverse graphs and how do they work?

A

1) Depth-first traversal - start at a given node and explore as far as possible along each branch before backtracking

2) Breadth-first traversal - explore closest nodes to starting node and then progressively exploring further nodes

41
Q

How does the depth-first graph traversal work?

A
  1. Recursively visits nodes that are adjacent to starting node
  2. Stops when visit a node with no unvisited adjacent nodes
  3. Backtracks to other unvisited adjacent nodes

Uses a stack to track each currently visiting node to allow backtracking

42
Q

How does breadth-first graph traversal work?

A
  1. Adds adjacent nodes to starting node to a queue
  2. Uses iteration to repeat the process till queue has all of its nodes dequeued as all nodes are visited
43
Q

What is a rooted tree?

A

A tree with the root node at the top

44
Q

What is a binary tree?

A

A rooted tree where each node has at most two child nodes stemming from it

45
Q

What is a hash table?

A

A data structure that stores a key-value pair based on an index calculated from the key from a hashing algorithm.

46
Q

What is the time complexity to search for an element in the hash table, given the key?

A

O(1) - this means it is not dependent on the number of items in the hash tables

47
Q

How do you add an item to a hash table?

A
  1. The key is processed by the hashing algorithm.
  2. An index is returned where the key-value pair will be stored unless this index of the hash table is not empty
  3. If not, linear probing or separate chaining can be used to deal with collisions.
48
Q

What is the process of linear probing?

A
  • used when a collision occurs
  • index is incremented until an empty index is found
  • however, it can lead to clustering where a few adjacent indexes are all filled whilst the rest are empty
  • also changes time complexity of searching back to O(n)
49
Q

What happens in separate chaining?

A

Each index stores a linked list - where each element of that list is items which have returned the same hash value

50
Q

What is rehashing?

A

Rehashing is the process of recognising when a hash table requires more space and exporting existing data to a new table with a new hash function to decrease collision probability.

51
Q

What can happen if collisions are not dealt with?

A

Data can be overwritten.

52
Q

What is a dictionary?

A

An abstract data type that stores a collection of key-value pair where any value can be accessed by its associated key.

53
Q

What are uses of dictionaries?

A

Dictionary based compression

54
Q

What is a vector?

A

Data structure that represents size (magnitude) and direction

55
Q

What is the dictionary representation of a vector?

A

Represents vector as a function
- each key-value pair stores the domain and co-domain

55
Q

How to represent a vector with components in reals and n components represented as?

A

ℝ^n

56
Q

What other ways can you represent a vector?

A
  • single-dimensional array
  • list representation
57
Q

How to add vectors?

A
  • vectors have to have same dimensions
  • add each component
58
Q

How to multiply a vector by a scalar?

A
  • multiplying each component by that scalar
59
Q

How to use the dot product?

A
  • multiply each corresponding component and add together to get a single number
  • a . b = |a||b|cosθ
  • used for getting angle
60
Q

What is the convex combination how to work it out?

A
  • line between two vectors
  • D = αA + βB
    α + β = 1
61
Q

What are uses of RPN?

A

stack-based interpreters as it is a postfix expression
- removes need for brackets

62
Q

How to solve RPN questions?

A
  • use a stack where you take out two operands when you get an operator
  • build a binary tree and do a post order traversal
  • convert to infix by using brackets
  • remember 2 5 / == 2 / 5
63
Q

What happens in linear search?

A
  • search for an element in an unordered set
  • iterate through each element to check if it is the element you are looking for
  • time complexity O(n) as time to search directly proportional to number of items in the dataset
64
Q

What are advantages and disadvantages of linear search?

A

+ works on unsorted and sorted lists
- inefficient O(n)

65
Q

What does efficiency depend on for linear search?

A
  • size of dataset
  • location of item to look for
66
Q

How does the binary search algorithm

A
  • works on an sorted list
    1) look at middle item (if even number, look at one up or down but keep consistent)
    2) compare with item and if greater, look at upper half, if lower, look at lower half
    3) repeated until found or low pointer is above the high pointer
67
Q

What are the advantages and disadvantages of binary search?

A

+ Very efficient for large databases - O(log n)
- Can be more difficult to implement

68
Q

How to do a binary tree search?

A

In-order traversal (use dots method)

69
Q

How does bubble sort work?

A

Each item in a list is compared to the one after it. If the item after is smaller that the current one then they swap. This process continues and runs through the list until no swaps are made and it is sorted.

70
Q

What are the disadvantages of bubble sort?

A
  • Inefficient time wise as time complexity of O(n^2) as it uses a nested for loop
  • Algorithm continues to iterate over the list even if no further swaps are required
71
Q

How can bubble sort be improved?

A
  • Reducing the number of comparisons after each pass
  • A sorted flag that can stop the sort if no swaps are made
72
Q

How does merge sort work?

A
  • Example of a divide and conquer algorithm where the program is broken down into smaller units
  • List is continuously divided until there is only one item in each
  • Sublists are combined in order until the list is fully sorted
73
Q

What are the disadvantages of merge sort?

A

If done recursively, is not very memory efficient

74
Q

What are the advantages of merge sort?

A

Time efficient with an efficiency of O(nlogn)

75
Q

What is an optimisation algorithm?

A

An algorithm that finds the best possible solution to the problem proposed.

76
Q

What is Dijkistra’s shortest path algorithms used for?

A
  • shortest path between two nodes in a graph
  • breadth-first search and keeps visited nodes in a priority queue
77
Q

What is the time complexity of Dijkistra’s?

A

O(n^2)

78
Q

What are the uses of Dijkistra’s?

A
  • shortest path between two locations in a transport network
  • network routing / packet switching
  • telephone and computer network planning