Data Structures 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;
}
}
~~~

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;
}
}
~~~

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

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.

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!”);
}
}
~~~

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.

27
Q

What are the uses for queues and stacks?

A
28
Q

What is a recursive subroutine?

A

A subroutine that is defined in terms of itself

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
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

31
Q

What is a graph?

A

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

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.