Week 10 + 11 + 12 Flashcards

1
Q

Define Map ADT.

A

Collection where each value is “mapped” to a unique key. You need a key-value pair to insert new mapping. You only need the key to find or remove mappings.

ADT Functions:
size()
isEmpty()
get(k)
put(k, v)
remove(k)
keySet()
entrySet()
values()

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

Explain unsorted map implementation. What is the time complexity?

A

We store the key-value pairs in a doubly linked list in an arbitrary order. O(n) linear search.

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

Explain hashing concept.

A

We implement Map as an array. Where key-values pairs are distributed evenly throughout the array by a hash function.

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

What two parts is the hash function a composition of?

A

Hash Code: keys -> integers
Compression Function: integers -> [0, N - 1]

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

How does Chain Hash Map handle collisions?

A

Map array is called a bucket array where each entry stores a pointer to another type of data structure. (Usually a linked list of AVL Tree). When a key-value pair maps to a bucket we simply add it to the data structure. We keep efficiency by keeping the size of these data structures small (i.e. less collisions).

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

How does Probe Hash Map handle collisions>

A

Open addressing is a collision resolution strategy where collisions are resolved by storing the
colliding key in a different location when the natural choice is full. Linear probing is an example of
open addressing, where we ‘probe’ linearly from the natural location until a suitable location is found. The advantage of this form is no auxiliary data structure is needed.

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

Give code for polynomial hashcode using constant N used on String?

A

int h = 0;
for(int i = 0; i < String.length(); i++){
h = h * N + ((int) String.charAt(i));
}
return h;

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

Code for MAD compression function.

A

(int) ((Math.abs(key.hashCode() * scale + shift) % prime) % capacity

Where prime > capacity
And scale, shift chosen at random from range [0,prime - 1]

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

What is collision?

A

Two keys mapping to the same location in a hash table.

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

How can collision be reduced?

A

A good hash function.

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

What is the time complexity of a Chain Hash Map.

A

O(ceiling(n / N))

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

What is the load factor?

A

l.f. = n / b = the ratio of the number of items in the table to the total number of buckets.

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

What is time complexity of Chain Hash Map when l.f. is below 1.

A

O(1)

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

What is the advantage of open addresing (probe hash map) over separate chaining (chain hash map)?

A

Don’t use an auxiliary data structure to hole entries with colliding keys. Open addressing has a much lower space complexity as a result. Benefits from caching as stored in memory sequentially.

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

Which type of hash map is ideal for a load factor above 1?

A

Chain Hash Map. Probe Hash Map cannot function if load factor is above 1.

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

Explain linear probing.

A

If try inset an entry into bucket A[j] but it is occupied we try A[j + 1 mod N] then A[j + 2 mod N] and so on. More collisions leads to longer sequence of probes.

If we try to get we must probe from given position until keys match.

When deleting we leave a special defunt marker so the probe skips over this element, later if key not found remembers this defunct as is a valid place to insert.

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

What is the formula for the expected number of probes?

A

1 / 1 - load factor

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

Name one advantage and one disadvantage of hashtables over other data structures.

A

A: Hashtables are much faster with larger tables or predictable datasets than other data structures.

D: Ordering not guaranteed. Inefficient when small number of entries or lots of collisions.

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

What is the BST property?

A

For a given node z, all nodes in the left subtree of z are less than z and all nodes in the right subtree of z are greater than z.

20
Q

Explain binary search.

A

Recursive algorithm, if node = value we are looking for terminate. Else search in the left subtree (if value < current node) or in the right subtree (if value > current node). If we reach a leaf, we return the position where we expected to find value.

21
Q

Define Sorted Map ADT.

A

A collection of unique key-value pairs where keys are maintained in sorted order. Supports accessing keys by relative position.

ADT functions.
firstEntry()
lastEntry()
ceilingEntry()
floorEntry()
lowerEntry()
HigherEntry()
subMap(k1, k2)

22
Q

What is the complexity of treeSearch?

A

O(h) where h is the height of the tree.

23
Q

Explain BST insertion.

A

Call tree search if node is found with given key override value else insert where treeSearch expected it to be found.

24
Q

Explain BST deletion.

A

Use treeSearch to find node to be deleted.
If leaf simply delete.
If only one child replace with child.
If two children replace with predecessor (guaranteeing at most only one child) and then replace with child or if no children delete.

25
Explain predecessor and successor methods.
Predecessor: the node in the rightmost position of the left subtree. Successor: The node in the leftmost position of the right subtree.
26
What is the expected height of a BST with elements inserted at random (without deletions)? Which means what? If we allow deletions what happens?
log n Time complexity of all TreeMap operations is O(log n) Time complexity of all TreeMap operations is O(root n)
27
Explain BST sort?
An inorder traversal of a BST returns elements in sorted order.
28
Define AVL Tree.
A TreeMap with an additional height-balance property stored in each node. The condition is that the height of the child subtrees of any node must differ by at most 1. The height-balance property = abs(height of left child - height of right child). This should be at most 1.
29
Explain right-right z.
Promote middle node y to be the root. The old root z becomes the left child of y. All nodes that were the left of y become the right child of z.
30
Explain left-left z.
Promote middle node y to be the root. The old root z becomes the right child of y. All nodes that were the right of y become the left child of z.
31
Explain left-right and right-left rotation.
left-right = right-right y and then left left z. Right-left. left-left y and then right-right z.
32
If we insert a node, and the height-balance property is violated where do we begin rotations from.
The first imbalance found from the inserted node is the node passed into the rotate method
33
Compare time complexity of AVL compared to BST.
While BST is O(h) because we can guarantee the height of AVL to be log n. All the operations are O(log n). subMap is O(s + log n) in AVL and O(s + h) in normal BST. where s is the number of elements in the resulting output.
34
What is the time complexity of size, isEmpty, entrySet and values for all trees?
isEmpty is O(1) size is O(1) entrySet is O(n) values is O(n)
35
Explain why subMap is O(s + h) time in a normal BST.
O(h) to find the position in the tree to start the inorder traversal. O(s) where s is the number of elements in the resulting output as this is exactly the number of nodes visited by this operation.
36
Define zig, zig-zig, and zig-zag operations.
zig: If x has no grandparent, we perform a single rotation at parent. zig-zig: First we rotate grand parent, then we rotate parent. zig-zag: First we rotate parent then we rotate grand-parent.
37
How do we insert into/get from a splay tree?
Insert leaf, then perform splay operation until inserted node is root. Same with get opertion.
38
How do we delete from a splay tree?
We delete a node in the same fashion as all BST. Then we splay the parent of the deleted node till it is the root.
39
Discuss splay tree performance.
After zig-zig or zig-zag, the depth of position p decreases by 2. After zig, the depth decreases by 1.
40
What is the time complexity of the splay operation?
O(d) where d is the depth of the position in the tree.
41
If I have a parent y with a left child x, what is the resulting structure after a zig operation.
A parent x with right child y.
42
A full binary tree has l leaves. How many nodes n does the tree have in terms of l.
l = n + 1 / 2 n = 2l - 1
43
Discuss the advantages and disadvantages of a linked list implementation of a queue compared with an array-based implementation of a queue.
An array-based queue will require an expensive resize operation when the queue is full. The linked list will grow naturally without explicit resizing. The linked list will have more memory overhead then the array based queue. Doesn't benefit from CPU caching as not stored sequentially in memory.
44
Is the root the biggest or smallest in a min-heap?
Smallest
45
Why is array based representation not suitable for an incomplete binary tree?
Lots of gaps leads to large waste of memory.
46
In a splay tree where do we start zig/zagging from?
The node inserted/the node we wish to splay.
47
Explain the process of deleting a node from an AVL Tree.
delete as from a regular BST, by finding node with treeSearch. If two children, swap with successor, then if no children delete, if one child overwrite with its child. We start from position p which is the child (possibly external i.e. null) of the removed node. There may be an imbalance on the path from p to the root (height-balance condition violated). Starting from p find the first unbalanced node (if there is one). We continue running rebalance operations until we reach the root of the tree. Every time we find an imbalance we perform one of the following rotations to rebalance the tree.