Data Structures Flashcards

1
Q

What is the height of a node in a tree?

A

the number of edges on the longest path between that node and a leaf.
any leaf node will have height 0

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

What is a tree?

A

it is a hierarchical data structure made of nodes and edges that:

  1. all nodes are connected
  2. there are no cycles
  3. each node has only one parent
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 3 ways if traversing a binary tree by depth?

A

PreOrder: evaluate the node once your reach it
InOrder: evaluate the node once you evaluated all left subtree
PostOrder: evaluate the node once you evaluated all right subtree

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

What is the average and worst time complexity of insert, search, delete on a binary search tree?

A

Average: O(h) where h is the tree height
Worst : O(n)

For all operations

O(h) is also O(logN)

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

What is a Full binary tree?

A

is a binary tree in which all nodes except leaves have two children

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

What is a Complete binary tree?

A

Binary Tree is complete if all levels are completely filled except possibly the last level and the last level has all keys as left as possible

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

What is a Perfect binary tree?

A

It is a binary tree in which all internal nodes have 2 children and every leaf node is on the same level.

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

What is the depth of a node in a tree?

A

is the number of edges from the node to the tree’s root node.
A root node will have a depth of 0.

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

What is a BST?

A

Binary Search Tree

  1. The left subtree of a node contains only nodes with keys less than the node’s key.
  2. The right subtree of a node contains only nodes with keys greater than the node’s key.
  3. Both the left and right subtrees must also be binary search trees.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the three abstract data types explained by Skiena? What are their difference?

A

Containers, dictionary and priority queue

Containers allow storage and retrieval of elements independent of the content (lists, arrays, trees)

Dictionaries stores and retrieves based on a key.

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

What is the advantage of priority queue over a normal sorted list?

A

Priority queue is more efficient for inserting items after everything is sorted.
It can be done in O(logn) time.

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

What are the two most common ways of collision resolution in a hash map? Explain.

A

Chaining: where we keep an array of linked lists. Waste memory with pointers on the linked list

Open memory: we keep an array of elements. When there is a collision we search for the next free spot. Deletion can be tricky.

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

What is the degree of a vertex/node in a graph?

A

It is the number of incident edges on a node, with loops counted twice.

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

What are the 3 main ways of representing graphs in programming? Explain each.

A
  • Adjacency List: Vertices are stored as objects, and every vertex stores a list of adjacent vertices. Allows the storage of additional data on the vertices because it is an object. Additional data can be stored if edges are also stored as objects, in which case each vertex stores its incident edges and each edge stores its incident vertices.
  • Adjacency Matrix: A two-dimensional matrix, in which the rows represent source vertices and columns represent destination vertices. Data on edges and vertices must be stored externally. Only the cost for one edge can be stored between each pair of vertices.
  • Incidence Matrix: A two-dimensional Boolean matrix, in which the rows represent the vertices and columns represent the edges. The entries indicate whether the vertex at a row is incident to the edge at a column.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which structure is better for representing sparse graphs? Why?

A

Adjacency List, because it uses space more effectively than the Adjacency Matrix, since the latter will have many empty spots in the matrix.

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

What is a heap?

A

A heap is a tree-based data structure that satisfies the heap property: if P is a parent node of C, then the value/key of P is greater than or equal the value of C (is the case of a max-heap), or is less than or equal the value o C (in the case of a min-heap).

17
Q

What data structure is important to implement the Dijkstra algorithm in a time-efficient way? Why?

A

The Min-Heap (min priority queue).

That is because at each iteration of the algorithm, we have to choose the vertex/node with the minimal path so far.

18
Q

Usually an adjacency matrix is implemented as a VxV array. How can I optimize the space used by the adjacency matrix?

A

By implementing the adjacency matrix using a hashmap keyed by the pair of vertices.

19
Q

What is the main advantage of using an adjacency matrix over an adjacency list?

A

It provides O(1) time for verifying if two vertices are connected.

20
Q

In which sort of situations a heap is indicated?

A

In algorithms in which, in each iteration, we find ourselves looking for a minimum of maximum. (e.g. merge k-sorted lists, Djikstra’s Shortest Path)

21
Q

How can we find the shortest path between two nodes in an undirected, unweighted graph?

A

Just run BFS until we find the destination node. That path will be the shortest path in that condition.

22
Q

How can we find a cycle in a graph?

A

Run BFS and keep track of visited nodes. If we visit a note twice, there is a cycle.

23
Q

What is graph coloring? And what is a legal coloring?

A

Coloring is assigning each node a color. Legal coloring is a coloring strategy in which adjacent nodes does not have the same color.

24
Q

What is the maximum degree of a graph?

A

Is the highest degree of all nodes.

25
Q

What is the chromatic number of a graph?

A

It is the lowest number of colors we can use to legally color a graph